2010-09-02 29 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

相關問題