2010-09-02 26 views
0

功能类:遍历解释:获取“NoneType”对象不是可迭代的错误

def play_best_hand(hand, wordDict): 

tempHand = hand.copy() 
points = 0 
for word in wordDict: 
    for letter in word: 
     if letter in hand: 
      tempHand[letter] = tempHand[letter] - 1 
      if tempHand[letter] < 0: 
       return False 
      if wordDict[word] > points: 
       bestWord == word 
       points = wordDict[word] 
return bestWord 

这是我引用错误209线对应于行。‘在wordDict’

Traceback (most recent call last): 
    File "ps6.py", line 323, in <module> 
    play_game(word_list) 
    File "ps6.py", line 307, in play_game 
    play_hand(hand.copy(), word_list) 
    File "ps6.py", line 257, in play_hand 
    guess = play_best_hand(hand, wordDict) 
    File "ps6.py", line 209, in play_best_hand 
    for word in wordDict: 
TypeError: 'NoneType' object is not iterable 
的字

回答

4

这意味着变量wordDictNone而不是字典,这意味着函数中有一个错误,调用play_best_hand。可能忘记return函数中的一个值,所以它返回None

+0

了“呃固定后,我开始打猎! 谢谢! – 2010-09-02 19:28:52

+0

为什么信会成为布尔? – 2010-09-02 19:35:24

2

play_best_hand()功能,您有:

if wordDict[word] > points: 
    bestWord == word 

你大概的意思做的,而不是平等比较分配:

if wordDict[word] > points: 
    bestWord = word 
+0

这是正确的。虽然,它没有解决这个问题。 – 2010-09-02 19:24:37

+3

@Noah Clark:可能即使有了这个更正,也可能会发生这样的情况:在返回之前,bestword没有被设置为任何东西。 (例如,如果'wordDict'为空或者'hand'为空字符串,就会发生这种情况。) – sth 2010-09-02 19:33:27

相关问题