【发布时间】:2021-07-22 17:57:10
【问题描述】:
我正在尝试制作一个琐事游戏,这是我目前拥有的代码。我在增加玩家在这场比赛中的得分时遇到了麻烦。我正在使用 while 循环来尝试增加玩家的分数,如果玩家 1 的问题错了,那么轮到玩家 2,但我不知道如何在两者之间切换。我有一个“答案”变量,玩家将在其中输入值。在这个 while 循环中,我想说的是,当“答案”等于正确答案时;我希望您执行以下操作,当“答案”不等于答案时,请执行以下操作。我正在尝试从我的答案字典中获取相应的值,但我不知道如何获取它。 “answers [key]”给我一个错误,我需要输入其他内容。
player_1_points = 0
player_1 = input("Enter in your name Player 1: ")
player_2_points = 0
player_2 = input("Enter in your name Player 2: ")
questions = {'one' : 'What is the tallest mountain in the world?'}
choices = {'a' : 'A. Mount Everest', 'b' : 'B. Mount St. Helens', 'c' : 'C. Mount Vesuvius', 'd' : 'D. K2'}
answers = {1 : {'Answer': 'A'}}
print(questions['one'])
print(choices['a'], choices['b'], choices['c'], choices['d'])
answer = str(input("Answer is: "))
while answer == answers[key] :
if player_1 == answer :
player_1_points += 1
print("Correct Answer!")
print(player_1, player_1_points)
print(player_2, player_2_points)
elif player_2 == answer :
player_2_points += 1
print("Correct Answer!")
print(player_1, player_1_points)
print(player_2, player_2_points)
while answer != answer[key] :
if player_1 != answer :
print("Incorrect, Player 2's Turn")
print(answer)
elif player_2 != answer :
print("Incorrect, Player 1's turn")
print(answer)
【问题讨论】:
-
首先,欢迎来到 StackOverflow ;) 其次,
key是什么?第三,answers有一个字典里面的字典有什么原因吗?
标签: python dictionary while-loop