编程错误“如果项目不在列表中”

时间:2014-07-21 17:19:10

标签: python python-2.7

我正在写一个文字游戏,玩家用x字母的手制作文字,当他们创造一个单词时,单词中的字母会从玩家手中减去。

我正在尝试使用此功能查看输入的单词是否在我的单词列表中:

def is_valid_word(word, hand, word_list):
    worddict = get_frequency_dict(word)
    if word not in word_list:
        return False

    for x in LETTERS:
        numlettershand = hand.get(x)
        numlettersword = worddict.get(x)
        if numlettershand == None and numlettersword != None:
            return False
        if numlettershand != None and numlettersword != None:
            if numlettershand - numlettersword < 0:
                return False

   return True

它给了我这个错误:

Traceback (most recent call last):
   File "C:\Users\Oleg\Desktop\Sams Stuff\programming\MIT\Problem Set 3\ps3a.py", line 112, in <module>
    play_hand(deal_hand(HAND_SIZE), load_words)
  File "C:\Users\Oleg\Desktop\Sams Stuff\programming\MIT\Problem Set 3\ps3a.py", line 106, in play_hand
    if is_valid_word(word, hand, wordlist) == True:
  File "C:\Users\Oleg\Desktop\Sams Stuff\programming\MIT\Problem Set 3\ps3a.py", line 75, in is_valid_word
    if word not in word_list:
TypeError: argument of type 'function' is not iterable

2 个答案:

答案 0 :(得分:1)

参数wordlist是一个函数,而不是可迭代的函数。您是否打算在将其传递给is_valid_word()之前调用它?

答案 1 :(得分:1)

您需要调用load_words:load_words()