我正在编写这个python代码,允许用户选择具有多种选择的简单和硬模式。每种模式的问题都是相同的,但硬版本只有更多选项可供选择。到目前为止,这是我的代码:
questions = ["What is 1 + 1",
"What is Batman's real name"]
answer_choices = ["1)1\n2)2\n3)3\n4)4\n5)5\n:",
"1)Peter Parker\n2)Tony Stark\n3)Bruce Wayne\n4)Thomas Wayne\n5)Clark Kent\n:"]
correct_choices = ["2",
"3",]
answers = ["1 + 1 is 2",
"Bruce Wayne is Batman"]
def quiz():
score = 0
for question, choices, correct_choice, answer in zip(questions,answer_choices, correct_choices, answers):
print(question)
user_answer = str(input(choices))
if user_answer in correct_choice:
print("Correct")
score += 1
else:
print("Incorrect", answer)
print(score, "out of", len(questions), "that is", float(score /len(questions)) * 100, "%")
quiz()
如果不制作新列表并且必须复制并粘贴所有内容,我将如何添加简单和更难?解释也不错。 提前感谢任何回复
答案 0 :(得分:2)
您可以创建所有问题的列表,然后根据难度将其拼接。
def get_choices(difficulty):
choices = [
"1)1\n2)2\n3)3\n4)4\n5)5\n:",
"1)Peter Parker\n2)Tony Stark\n3)Bruce Wayne\n4)Thomas Wayne\n5)Clark Kent\n:"
]
if difficulty == 'easy':
choices = [c.split("\n")[:3] for c in choices]
return choices
elif difficulty == 'medium':
choices = [c.split("\n")[:4] for c in choices]
return choices
else:
return choices
如果您可以将每个单独的选项作为列表元素并且具有与之对应的解决方案,那么它将更简单。然后,您可以获得正确的解决方案并随机播放其他答案并自动分配号码。
答案 1 :(得分:0)
您可以定义功能块,并根据用户输入调用它们:
# define the function blocks
def hard():
print ("Hard mode code goes here.\n")
def medium():
print ("medium mode code goes here\n")
def easy():
print ("easy mode code goes here\n")
def lazy():
print ("i don't want to play\n")
# Now map the function to user input
choose_mode = {0 : hard,
1 : medium,
4 : lazy,
9 : easy,
}
user_input=int(input("which mode do you want to choose : \n press 0 for hard \n press 1 for medium \n press 4 for lazy \n press 9 for easy "))
choose_mode[user_input]()
然后调用功能块:
choose_mode[num]()
答案 2 :(得分:0)
实现类似的方法的一种方法是从列表开始,对于每个问题,包含正确的答案和可能的错误答案。
然后,您将根据难度级别,通过为每个问题选择正确数量的错误问题,创建将从该基本列表生成实际问题列表的代码。然后,生成的新列表将用于询问问题。