首页 > 解决方案 > 如何查看玩家是否回答了与该值的键对应的值

问题描述

编写一个简单的问答游戏,其中包含十个问题的列表和这些问题的答案列表。游戏应该给玩家四个随机选择的问题来回答。它应该一个一个地问问题,并告诉玩家他们的问题是对还是错。最后它应该打印出四分之几他们做对了。

我创建了一个从字典中选择一个随机键的函数。我的字典由 10 个键组成('a' 国家的首都是什么),每个键都有一个答案(那个国家的首都'a')。想象一下我有(现在有以下 3 个功能)。

import random

questions = {'What is the capital of Portugal':'Lisbon','What is the capital of France':'Paris','What is the capital of Germany':'Berlin','What is the capital of Netherlands':'Amsterdam','What is the capital of Belgium':'Brussels','What is the capital of Nepal':'Kathmandu','What is the capital of Spain':'Madrid','What is the capital of England':'London','What is the capital of Ireland':'Dublin','What is the capital of United_States':'Washington'} #dictionary with all questions and answers

#function that returns random question from dict
def questions_sorting():

  return random.sample(list(questions.keys()),1)

print(questions_sorting())


#function that asks player for capital input
def player_answer():

  return input('Digit your final answer - \n')

def countries_game():

  right_ans = 0 #counts the number of right answers 
  wrong_ans = 0 #counts the number of wrong answers
  num_questions = 0 #counts the number of questions displayed so far

  while num_questions < 4: #there will be four questions to be displayed
    cpu_question = questions_sorting()
    player_choice = player_answer()
    num_questions += 1

我如何查看玩家是否为询问的键输入了正确的值?同样在我的问题排序功能中,我正在对一个问题进行排序。我怎么能问另一个问题而不能问同样的问题?

标签: pythondictionaryrandom

解决方案


您可以player_choice通过将其与通过提供问题作为键获得的答案进行比较来检查答案是否为答案。然后,删除问题,这样您就不会问同样的问题。

import random

#function that returns random question from dict
def questions_sorting(questions):
    # [0] because you don't want to ask a question with a list
    return random.sample(list(questions.keys()),1)[0] 

#function that asks player for capital input
def player_answer():
    return input('Digit your final answer - \n')

def countries_game():
    questions = {'What is the capital of Portugal': 'Lisbon', 'What is the capital of France': 'Paris',
                 'What is the capital of Germany': 'Berlin', 'What is the capital of Netherlands': 'Amsterdam',
                 'What is the capital of Belgium': 'Brussels', 'What is the capital of Nepal': 'Kathmandu',
                 'What is the capital of Spain': 'Madrid', 'What is the capital of England': 'London',
                 'What is the capital of Ireland': 'Dublin',
                 'What is the capital of United_States': 'Washington'}  # dictionary with all questions and answers
    right_ans = 0 #counts the number of right answers
    wrong_ans = 0 #counts the number of wrong answers
    num_questions = 0 #counts the number of questions displayed so far
    while num_questions < 4: #there will be four questions to be displayed
        cpu_question = questions_sorting(questions)
        print(cpu_question)
        player_choice = player_answer()
        if player_choice==questions[cpu_question]:
            print('correct')
            right_ans+=1
        else:
            print('incorrect')
            wrong_ans+=1
        num_questions += 1
        del questions[cpu_question]
    print(f'correct: {right_ans}, incorrect: {wrong_ans}')
countries_game()

推荐阅读