Python 如果传递了特定的输入并跳过了代码的结尾,则尝试立即重新启动方法
我是python的新手,如果输入不正确(e-z的任何字母,包括e和z),我会尝试实现强制重启功能Python 如果传递了特定的输入并跳过了代码的结尾,则尝试立即重新启动方法,python,Python,我是python的新手,如果输入不正确(e-z的任何字母,包括e和z),我会尝试实现强制重启功能 我如何让它放弃之前所有的测验答案,并在通过不需要的输入时重新启动该方法?使用for–else answers=[] 通过=错误 虽然没有通过: 有关问题: 答案=输入('输入你的答案') 如果(不是有效的)回答(回答)): #重新开始。 答案=[] 打破 答案。附加(答案)#或其他什么 其他: 通过=正确#没有中断,因此通过。 处理你的答案(答案) 以下是我重写的内容:-我仍然有problems@r
我如何让它放弃之前所有的测验答案,并在通过不需要的输入时重新启动该方法?使用
for–else
answers=[]
通过=错误
虽然没有通过:
有关问题:
答案=输入('输入你的答案')
如果(不是有效的)回答(回答)):
#重新开始。
答案=[]
打破
答案。附加(答案)#或其他什么
其他:
通过=正确#没有中断,因此通过。
处理你的答案(答案)
以下是我重写的内容:-我仍然有problems@r0wdy检查缩进。您应该在for
循环中检查答案的有效性。
from Question import Question
incorrect_input = False
incorrect_final = False
exit_test = False
question_prompts = [
"What colors are pears?\n(a) Red\n(b) Blue\n(c) Green\n(d) Yellow",
"What colors are bananas?\n(a) Yellow\n(b) Blue\n(c) Green\n(d) Red",
"What colors are apples?\n(a) Blue\n(b) Yellow\n(c) Red/Green\n(d) Orange"
]
questions = [
Question(question_prompts[0], "c"),
Question(question_prompts[1], "a"),
Question(question_prompts[2], "c")
]
def exec_test(questions):
score = 0
incorrect_input = False
incorrect_final = False
exit_test = False
for question in questions:
answer = input(str(question.prompt) + "\nEnter your answer: ")
if answer == question.answer and answer.lower() not in "efghijklmnopqrstuvwxyz": # incorrect input check
print("Correct Answer!\n")
score += 1 # increments score by one if correct
elif answer != question.answer and answer.lower() not in "efghijklmnopqrstuvwxyz": # incorrect input check
print("Incorrect Answer!\n")
else:
print("You entered an incorrect form of input! Try again.\n")
incorrect_input = True # sets a boolean to True which eventually restarts the quiz
incorrect_final = True
exit_test = True
return incorrect_final
if exit_test is True:
return None
if incorrect_final is True:
exec_test(questions)
print("Final Score: " + str(score) + "/" + str(len(questions)) + " correct!") # final score output
exec_test(questions)