【发布时间】:2016-07-22 18:22:50
【问题描述】:
这是我的代码
players = []
while len(players) >= 0:
name = input('Enter a name: ')
players.append(name)
if name == '':
players.pop()
break
else:
pass
player_dict = {name: [] for name in players}
print(player_dict)
for name in player_dict:
answer = input(name + ', who will win the fight? ')
player_dict[name].append(answer)
fight_winner = input('Who won the fight? ')
for name in player_dict:
if answer == fight_winner:
print(name + ' = Correct')
else:
print(name + ' = Incorrect')
print(player_dict)
这是我运行代码时看到的
Enter a name: bill
Enter a name: bob
Enter a name:
{'bill': [], 'bob': []}
bill, who will win the fight? red
bob, who will win the fight? blue
Who won the fight? red
bill = Incorrect
bob = Incorrect
{'bill': ['red'], 'bob': ['blue']}
我希望看到bill = Correct。如何访问每个个体值并通过 if 语句运行它?感谢您的帮助
【问题讨论】:
-
你可能想要
if fight_winner in player_dict[name]:这样的东西,但我不明白为什么 player_dict 值是列表。
标签: python-3.x if-statement dictionary