我想编写一个程序,询问用户的问题,然后检查他的答案。这里显示3个问题和3个答案。我能够把它写成一个单一的问题 - 答案对,但我很难在所有对中使用相同的代码片段 - 我不想为每个问题 - 答案对使用显式代码,因为它会生成大量的code.I非常感谢帮助让它适用于所有问题。
Q1 = "Question 1 Is 30 fps more cinematic than 60 fps? - a)Yes, b)No, c)It's a matter of opinion or d)Nobody knows"
A1 = "b"
Q2 = "Question 2 Test Question? - a), b), c) or d)"
A2 = "b"
Q3 = "Question 3 If 10 + 9 is 19 what is 9 + 10? - a)19, b)20, c)21 or d)22"
A3 = "c"
def quiz():
n = 0
while n < 3:
n = n + 1
if str(input(Q1)) == A1:
print("Correct answer")
else:
print("Wrong Answer, Correct Answer is", A1)
print(quiz())
答案 0 :(得分:1)
使用列表为您提供问题和答案,并使用zip
进行迭代。
qus = [Q1, Q2, Q3, ...]
ans = [A1, A2, A3, ...]
def quiz():
for q, a in zip(qus, ans):
if str(input(q)) == a:
print ("Correct answer")
else:
print("Wrong Answer, Correct Answer is", a)
quiz()
答案 1 :(得分:1)
你可以用类似的数据结构来做到这一点。这也将处理不同的问题风格。
questions = [{"question": "Is 30 fps more cinematic than 60 fps?",
"answers": {"a": "Yes",
"b": "No",
"c": "It's a matter of opinion",
"d": "Nobody knows"},
"correct": "b"},
{"question": "Question 2 Test Question?",
"answers": {"a": "",
"b": "",
"c": "",
"d": ""},
"correct": "b"},
{"question": "Question 3 If 10 + 9 is 19 what is 9 + 10?",
"answers": {"a": "19",
"b": "20",
"c": "21",
"d": "22"},
"correct": "c"},
]
def quiz():
for question in questions:
print(question["question"])
for choice, answer in question["answers"].iteritems():
print("{:s}: {:s}".format(choice, answer))
if str(input()) == question["correct"]:
print("Correct answer")
else:
print("Wrong Answer, Correct Answer is", question["correct"])
quiz()