【问题标题】:How to use a dictionary inside of a while loop?如何在while循环中使用字典?
【发布时间】: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


【解决方案1】:

抱歉,我更改了您的代码。

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: ")

# now questions is a list of dictionaries
questions = [
    {
        'question': 'What is the tallest mountain in the world?',
        'option a': 'A. Mount Everest',
        'option b': 'B. Mount St. Helens',
        'option c': 'C. Mount Vesuvius',
        'option d': 'D. K2',
        'answer': 'A. Mount Everest'
    }
]

# you can add more questions to above list of questions


# loop through list of questions 
for question in questions:
    print(question['question'])
    print(question['option a'])
    print(question['option b'])
    print(question['option c'])
    print(question['option d'])

    # ask player 1 to enter their answer
    player_1_answer = input("Enter you answer Player 1 : ")
    if player_1_answer == question['answer']: 
        print('Correct Answer!')
        player_1_points += 1
    else:
        print('Incorrect Answer!')

    # ask player 2 to enter their answer
    player_2_answer = input("Enter you answer Player 2 : ")
    if player_2_answer == question['answer']:
        print('Correct Answer!')
        player_2_points += 1
    else:
        print('Incorrect Answer!')

# print scores for each player at the end
print(player_1, player_1_points)
print(player_2, player_2_points)

【讨论】:

  • 感谢您的帮助,但我想知道您是否可以使用 while 循环来做到这一点。
  • 我想在一本词典中有问题,在另一本词典中有选择,在另一本词典中有答案,这样我可以更轻松地引用它们。我必须把所有东西都放在一本字典里吗?
  • 我把所有的都放在一本字典里,因为它更容易访问(对我来说)。您可以将它们放在不同的字典中。
猜你喜欢
  • 2021-09-09
  • 2021-10-05
  • 1970-01-01
  • 1970-01-01
  • 2017-07-18
  • 2019-06-08
  • 1970-01-01
  • 2020-12-04
  • 2014-11-01
相关资源
最近更新 更多