问题
Why will the break not end the while true and return to the start?
while True:
print('This is a quiz')
print('What is your name?')
Name = input()
print('Hello ' + Name + ', The quiz will now begin')
import time
time.sleep(2)
question1 = "Question one: "
answer1 = "True" and "true"
print(question1)
qanswer = input()
if qanswer != answer1:
print('Sorry, the answer is: ' + answer1)
break
if answer1 == qanswer:
print("Correct! Here's the next question")
I'm pretty new to python so I assume it's just a simple misuse of the terms.
回答1:
break
exists the entire while
loop.
continue
is what you're looking for, returning to the next while
iteration.
You can read more about control flow tools, break
and continue
in the official docs: http://docs.python.org/2/tutorial/controlflow.html#break-and-continue-statements-and-else-clauses-on-loops
(You also have some other bugs as others have mentioned correctly)
回答2:
use the "continue" keyword, not "break".
回答3:
....
answer1 = ["True", "true"]
...
if not(qanswer in answer1):
print('Sorry, the answer is: ' + answer1)
break
else:
print("Correct! Here's the next question")
来源:https://stackoverflow.com/questions/13962471/python-break-function-doesnt-end-while-true