【发布时间】: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