我只是一个初学者,但是我的代码遇到了麻烦。 我正在尝试从列表中获取正确答案,但不会打印错误或正确纠正
# List of questions in Quiz
question_List = ["How do you write number 1 in Maori?\n1.Tekau 2.Tahi 3.Ono 4.Rua",
"What is does tahi + tahi = ?\n1.Rua 2.Rimu 3.Ono 4.Tahi",
"How do you write blue in Maori?\n1.Kakariki 2.Kikorangi 3.Whero 4.Ma",
"What two colours make blue?\nMa + Whero 2.Kikorangi + Kowhai 3.Whero + Pararui 4.Ma + Mangu",
"Who was the god of the forest and birds?\n:1.Ranginui 2.Paptuanuku 3.Tane-Mahuta 4.Tangaroa",
"Who were Tane Mahutas Parents?\n1.Tangaroa + Ranguinui 2.Punga + Ranganui 3.Tangaroa + Rongo 4.Papatunuku + Ranganui"]
# List of Correct Answers
correct_Answer = [ "2", "1", "2", "2","3","4"]
# If user enters anything that is not an integer between 1 and 4
def intcheck(question, low, high):
valid= False
while not valid:
error= "Whoops! Please enter an integer between {} and {}".format(low, high)
try:
response = int(input("Please enter an integer between {} and {}:".format(low, high)))
if low <= response <= high:
return response
else:
print(error)
print()
except ValueError:
print(error)
# Get a question from the question list and print and loop
for question in question_List:
print(question)
Answer = intcheck("Please enter in an answer or press enter to quit", 1,4)
print()
for Answer in correct_Answer:
print("correct")
else:
print("incorrect")
答案 0 :(得分:0)
您可能只需要将最后一个for
语句更改为if
。
编辑:
另外,您的correct_Answer
列表应包含整数而不是字符串:
correct_Answer = [2, 1, 2, 2, 3, 4]
编辑2:
您想将每个答案与相应的正确答案进行比较,请参见下面的代码:
# Get a question from the question list and print and loop
for idx, question in enumerate(question_List):
print(question)
Answer = intcheck("Please enter in an answer or press enter to quit", 1,4)
print()
if Answer == correct_Answer[idx]:
print("correct")
else:
print("incorrect")