Python 无限循环,直到三个条件之一为真。普顿
我对编程非常陌生,所以可能只是我遇到的基本语法问题,或者可能对函数不熟悉,这限制了我的能力,但问题是。我在youtube的一个教程中发现了以下程序,在该教程中,导师创建了一个多项选择题测试,使用class方法给出了3个可能的答案。以下是原始代码:Python 无限循环,直到三个条件之一为真。普顿,python,conditional-statements,Python,Conditional Statements,我对编程非常陌生,所以可能只是我遇到的基本语法问题,或者可能对函数不熟悉,这限制了我的能力,但问题是。我在youtube的一个教程中发现了以下程序,在该教程中,导师创建了一个多项选择题测试,使用class方法给出了3个可能的答案。以下是原始代码: class Question: def __init__(self, prompt, answer): self.prompt = prompt self.answer = answer question_p
class Question:
def __init__(self, prompt, answer):
self.prompt = prompt
self.answer = answer
question_prompts = [
'What color are apples?\n(a) Red/Green\n(b) Purple\n(c) Orange\n\n',
'What color are Bananas?\n(a) Teal\n(b) Magenta\n(c) Yellow\n\n',
'What color are Strawberries?\n(a) Yellow\n(b) Red\n(c) Blue\n\n',
]
questions = [
Question(question_prompts[0], 'a'),
Question(question_prompts[1], 'c'),
Question(question_prompts[2], 'b'),
]
def run_test(questions):
score = 0
for question in questions:
answer = input(question.prompt)
if answer == question.answer:
score +=1
print('You got ' + str(score) + '/' + str(len(questions)) + ' correct')
run_test(questions)
原始代码允许用户只尝试每个问题一次,最终在总共3个问题中得分
问题是:
我想添加一个条件语句/循环(在if语句计分之前),以检查用户是否输入了正确的“a”或“b”或“c”选项。如果用户输入的不是a、b或c,那么它必须无限地留在循环中,并要求用户输入正确的a、b或c选项
我尝试了while循环,但它并没有中断,即使用户输入了正确的a、b或c选项。请提供帮助您可以在无限while循环中添加if条件来打破它 另外,您的
run\u test()
函数必须返回分数,请参见以下内容:
class Question:
def __init__(self, prompt, answer):
self.prompt = prompt
self.answer = answer
def run_test(questions):
score = 0
for question in questions:
while True:
answer = input(question.prompt)
if answer in ['a', 'b', 'c']:
break
if answer == question.answer:
score +=1
return score
question_prompts = ['What color are apples?\n(a) Red/Green\n(b) Purple\n(c) Orange\n\n',
'What color are Bananas?\n(a) Teal\n(b) Magenta\n(c) Yellow\n\n',
'What color are Strawberries?\n(a) Yellow\n(b) Red\n(c) Blue\n\n']
questions = [Question(question_prompts[0], 'a'),
Question(question_prompts[1], 'c'),
Question(question_prompts[2], 'b')]
score = run_test(questions)
print("\nYou got {}/{} correct".format(score, len(questions)))
样本输出:
What color are apples?
(a) Red/Green
(b) Purple
(c) Orange
a
What color are Bananas?
(a) Teal
(b) Magenta
(c) Yellow
b
What color are Strawberries?
(a) Yellow
(b) Red
(c) Blue
c
You got 1/3 correct
您可以在for循环中使用while循环,并对每个新问题进行设置,以便仅在输入错误时循环 这应该是你要找的
class Question:
def __init__(self, prompt, answer):
self.prompt = prompt
self.answer = answer
score = 0
question_prompts = [
'What color are apples?\n(a) Red/Green\n(b) Purple\n(c) Orange\n\n',
'What color are Bananas?\n(a) Teal\n(b) Magenta\n(c) Yellow\n\n',
'What color are Strawberries?\n(a) Yellow\n(b) Red\n(c) Blue\n\n',
]
questions = [
Question(question_prompts[0], 'a'),
Question(question_prompts[1], 'c'),
Question(question_prompts[2], 'b'),
]
def run_test(questions):
score = 0
for question in questions:
wrong = True
while wrong == True :
answer = input(question.prompt)
if answer == question.answer:
score +=1
wrong = False
print('You got ' + str(score) + '/' + str(len(questions)) + ' correct')
run_test(questions)
首先,当您在最终打印行中引用
分数时,此程序将崩溃。这是因为score
是run\u test()
函数内部的一个局部变量,在该函数外部无法访问,有很多方法可以解决这个问题,但是一个快速而肮脏的方法就是将score=0
移到run\u test()
外部,然后移到run\u test()内部
addglobal score
,这意味着score与print语句存在于相同的“范围”中,global score
语句意味着run\u test()
将知道如何在其范围之外查找要使用的变量
现在,对于您的实际问题,您可以非常简单地通过在答案输入周围添加while True:
并在答案正确时调用break
来解决此问题
您当前还应该为每个问题增加分数,而不仅仅是在最后,因此应该将分数插入for
循环中
最后,在实际调用计算出分数的函数之前,您正在引用score
;即使该函数是在另一位代码上面定义的,该函数中的代码在调用该函数之前不会运行
下面是一段工作代码:
class Question:
def __init__(self, prompt, answer):
self.prompt = prompt
self.answer = answer
question_prompts = [
'What color are apples?\n(a) Red/Green\n(b) Purple\n(c) Orange\n\n',
'What color are Bananas?\n(a) Teal\n(b) Magenta\n(c) Yellow\n\n',
'What color are Strawberries?\n(a) Yellow\n(b) Red\n(c) Blue\n\n',
]
questions = [
Question(question_prompts[0], 'a'),
Question(question_prompts[1], 'c'),
Question(question_prompts[2], 'b'),
]
score = 0
def run_test(questions):
global score
for question in questions:
while True:
answer = input(question.prompt)
if answer == question.answer:
print("Correct!")
score +=1
break
else:
print("Incorrect, please try again")
run_test(questions)
print('You got ' + str(score) + '/' + str(len(questions)) + ' correct')
您发布的代码是您尚未编写的代码,它可以正常工作,但引用的代码是您编写的代码,它无法正常工作。为什么不发布您编写的代码,因为这是有实际问题的代码?请阅读有关如何创建堆栈的信息。欢迎使用堆栈溢出!看看这本书。正如约翰所说,我们不是读心术的人。您需要将非工作代码作为一个组件提供。请参阅其他提示。仔细想想,除了其他人所说的(我同意)之外,这可能是重复的-不要害怕分享“坏代码”;这里的人往往乐于帮助你提高,通过分享你的尝试,你会学到最多,即使你还不为他们感到骄傲。还有一点,你的列表末尾有一个额外的“,”是不必要的。非常感谢,它真的帮助我理解了while的逻辑loop@ahsanmbutt很高兴这有帮助。如果您觉得有帮助,请踊跃投票和/或接受我的答案(勾选)。