代码之家  ›  专栏  ›  技术社区  ›  Noah Clark

迭代字典:Get'NoneType'对象不是iterable错误

  •  0
  • Noah Clark  · 技术社区  · 15 年前

    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
    

    这是我的trackback错误。第209行对应于“for word in 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
    
    2 回复  |  直到 15 年前
        1
  •  4
  •   leoluk    15 年前

    这意味着变量 wordDict None 而不是字典。这意味着调用 play_best_hand return 没有 ?

        2
  •  2
  •   sth    15 年前

    play_best_hand() 您拥有的功能:

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

    你可能是想做一个作业,而不是进行平等比较:

    if wordDict[word] > points:
        bestWord = word
    
    推荐文章