Python字典for循环无值

时间:2013-11-17 18:19:44

标签: python dictionary

我的代码:

hand = {'e': 1, 'i': 2, 'h': 1, 's': 1, 'r': 1, 'w': 1, 'v': 2}
def showhand(a):
    for letter in hand:
        for j in range(hand[letter]):
            print letter,

我期望获得的输出是:

e i i h s r w v v 

但我看到的是:

e i i h s r w v v None

我不知道None值的来源。如何获得我想要的输出?

1 个答案:

答案 0 :(得分:6)

你必须这样调用这个函数:

print showhand(...)

这样做会导致Python打印showhand的返回值,即None

要解决您的问题,请移除print并按照以下方式调用您的函数:

showhand(...)
相关问题