我注意到它是一种流行的TypeError,但我找不到解决方案。看看我的代码并说出错了,请:
import random
players_list = []
def add_player():
possible_flags = {'is_computer', 'is_human'}
decision = -1
while decision != '0':
player_name = input('Give a name: ')
while player_flag not in possible_flags
player_flag = input("Type 'is_computer' or 'is_human': ") #is_computer or is_human
players_list.append({'player_name': player_name, 'player_flag': player_flag, 'player_decision': None})
decision = input('Do you want add a new player [1]? To leave type [1] ')
def play():
options = {1: 'rock', 2: 'paper', 3: 'scissors', 4: 'lizard', 5: 'Spock'}
for i in players_list:
if('is_computer' in players_list[i]):
player_decision = random.choice(options)
elif('is_player' in players_list[i]):
player_decision = input()
player_list[i]['player_decision'] = options[player_decision]
add_player()
play()
问题在于:
if('is_computer' in players_list[i]):
我也试过了:
if('is_computer' in players_list[i]['player_flag']):
但这也行不通。 我只是不明白。我把dict放在列表中是否犯了错误?我会很感激任何关于它的文章的链接。
非常感谢
答案 0 :(得分:2)
for i in players_list
i
不是索引,而是元素本身。您可能希望将代码段重写为:
for player in player_list:
if player['player_flag'] == 'is_computer':
...