【问题标题】:How do I make the same code work for multiple dictionaries?如何使相同的代码适用于多个字典?
【发布时间】:2021-08-07 09:03:00
【问题描述】:

我是 python 新手,我做了一个测验,询问用户他们是否想玩简单、中等或困难的游戏,然后代码会根据用户选择的难度显示问题。我有字典中的所有问题和答案(每个难度都有一本字典,例如用于简单问题和答案的字典和用于困难问题和答案的字典等),并且我有这部分代码来检查用户的答案是否正确或错误。问题是检查答案是否正确的代码部分一次只能使用一个字典。另一个问题是我不知道如何使它,如果用户选择简单,那么代码会从简单的字典中打印出简单的问题。if answer.lower() == Question_list_easy[ques]: 部分只允许 1 个字典,这就是为什么我认为代码只有一次允许一个字典。我试过if chooses == "easy": print (Question_list_easy),但它不起作用。请帮帮我。

import random

quiz_difficulty = input("Would you like to play on easy, medium, or hard\n").lower()
if quiz_difficulty == "easy":
  print (Question_list_easy)
   
if quiz_difficulty == "medium":
   print (Question_list_medium)

if quiz_difficulty == "hard":
   print (Question_list_hard)


Question_list_easy = {
   "How many days are there in a year?":"365",
   "How many hours are there in a day?":"24", 
   "How many days are there in a week?":"7"
   } 

Question_list_medium = {
   "How many weeks are there in a year?":"52",
   "How many years are there in a decade?":"10", 
   "How many days are there in a fortnight?":"14"
   } 

Question_list_hard = {
   "How many years are there in a millennium?":"1000",
   "How many days are there in 2 years?":"730", 
   "How many years are there in a half a century?":"50"
   } 
  


#The part which asks the questions and verifies if the answer the user does is coreect or incorrect. It also randomizes the questions
question = list (Question_list_easy.keys())
#input answer here 
while True:
    if not question:
        break
    ques = random.choice(question)
    print(ques)
    while True:
        answer = input('Answer ' )
        # if correct, moves onto next question
        if answer.lower() == Question_list_easy[ques]:
            print("Correct Answer")
            break
        else:
            #if wrong, Asks the same question again
            print("Wrong Answer, try again")
    question.remove(ques)

【问题讨论】:

  • 与问题无关,千年是1000年

标签: python dictionary


【解决方案1】:

尽管有些问题的答案是错误的,但这里有一种方法可以用更少的代码实现您的目标。这里的关键特征是使用字典,其中键/值对是映射到适当问题字典的难度级别。这消除了在选择难度级别时对 if/then/else 的需求,因此如果您想添加更多难度级别,则更具可扩展性:-

Question_list_easy = {
    "How many days are there in a year?": "365",
    "How many hours are there in a day?": "24",
    "How many days are there in a week?": "7"
}

Question_list_medium = {
    "How many weeks are there in a year?": "52",
    "How many years are there in a decade?": "10",
    "How many days are there in a fortnight?": "14"
}

Question_list_hard = {
    "How many years are there in a millennium?": "365",
    "How many days are there in 2 years?": "730",
    "How many days are there in a half a century?": "50"
}

qmap = {'easy': Question_list_easy,
        'medium': Question_list_medium,
        'hard': Question_list_hard}

questions = None

while questions is None:
    inp = input('Would you like to play easy, medium, or hard? ')
    questions = qmap.get(inp)

while len(questions) > 0:
    _i = random.randint(0, len(questions) - 1)
    _key = list(questions)[_i]
    inp = input(_key+' ')
    if inp == questions[_key]:
        print('Correct')
        questions.pop(_key)
    else:
        print('Incorrect. Try again')

【讨论】:

    【解决方案2】:

    这看起来很接近,但我认为你缺少的部分是你可以说:

    if quiz_difficulty == "easy":
       question_list = Question_list_easy
    if quiz_difficulty == "medium":
       question_list = Question_list_medium
    if quiz_difficulty == "hard":
       question_list = Question_list_hard
    

    然后,在您提出问题的部分中,您只需使用 question_list 而不是专门引用 Question_list_easy

    【讨论】:

    • 你是个天才!我真的无法感谢你
    【解决方案3】:

    将提出问题的部分放入函数中,并将适当的dict传递给函数,如下所示:

    def ask_the_questions(q_dict):
        #The part which asks the questions ons
        question = list (q_dict.keys())
        ... etc ...
    

    然后用合适的dict调用函数,例如:

    ask_the_question(Question_list_easy)
    

    【讨论】:

      【解决方案4】:

      我已将您的代码更改如下:

      question = ''
      question_set = ''
      if quiz_difficulty == "easy":
        question = list (Question_list_easy.keys())
        question_set = Question_list_easy
         
      if quiz_difficulty == "medium":
         question = list (Question_list_medium.keys())
         question_set = Question_list_medium
      
      if quiz_difficulty == "hard":
         question = list (Question_list_hard.keys())
         question_set = Question_list_hard
      
      while True:
          if not question or not question_set:
              break
          ques = random.choice(question)
          print(ques)
          while True:
              answer = input('Answer ' )
              # if correct, moves onto next question
              if answer.lower() == question_set[ques]:
                  print("Correct Answer")
                  break
              else:
                  #if wrong, Asks the same question again
                  print("Wrong Answer, try again")
          question.remove(ques)
      

      【讨论】:

        猜你喜欢
        • 2022-10-01
        • 2022-11-21
        • 2019-12-02
        • 2020-11-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-03-15
        相关资源
        最近更新 更多