解决此错误? TypeError:类型'builtin_function_or_method'的参数不可迭代

时间:2019-10-20 15:15:27

标签: python list dictionary iterator

我正在尝试查找变量是否在列表内的字典的键列表中。我只是使用常规的字典引用来完成此操作的,但没有用。

我以前把所有的三个字典都排好了,但没有用。现在它们都分开了。

 if currentTurn == 1:
        while len(player1sequence1) != 5:
            addingCard = str(input("Type the code of the card you want to add to your sequence:"))
            if addingCard in player1Cards[0].keys:
                if player1Cards[0][addingCard] != 0:
                    player1Cards[0][addingCard] -= 1
                    player1sequence1.append(addingCard)
                else:
                    print("You ran out of that card. Try again.")
            elif addingCard in player1Cards[1].keys:
                if player1Cards[1][addingCard] != 0:
                    player1Cards[1][addingCard] -= 1
                    player1sequence1.append(addingCard)
                else:
                    print("You ran out of that card. Try again.")
            elif addingCard in player1Cards[2].keys:
                if player1Cards[2][addingCard] != 0:
                    player1Cards[2][addingCard] -= 1
                    player1sequence1.append(addingCard)
                else:
                    print("You ran put of that card. Try again.")
        print(player1sequence1)

我希望结果将“ addingCard”添加到列表“ player1sequence1”,但它使我犯了这个错误:

 Traceback (most recent call last):
  File "C:/Users/Khang Nguyen/Attack Sequence text Based version.py", line 263, in <module>
    if addingCard in player1Cards[0].keys:
TypeError: argument of type 'builtin_function_or_method' is not iterable

1 个答案:

答案 0 :(得分:0)

keys是函数,而不是属性,因此应添加()

if addingCard in player1Cards[0].keys():

甚至更好:

if addingCard in player1Cards[0]: