1

为什么 break 不会在 while true 结束并返回开始?

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")

我对python很陌生,所以我认为这只是对术语的简单滥用。

4

3 回答 3

5

break存在整个while循环。

continue是您正在寻找的,返回到下一个while迭代。

您可以阅读有关控制流工具的更多信息,breakcontinue在官方文档中: http: //docs.python.org/2/tutorial/controlflow.html#break-and-continue-statements-and-else-clauses-on-loops

(您还有其他一些错误,正如其他人正确提到的那样)

于 2012-12-19T22:43:53.103 回答
0

使用“continue”关键字,而不是“break”。

于 2012-12-19T22:44:45.277 回答
0
....
answer1 = ["True", "true"]
...
if not(qanswer in answer1):
    print('Sorry, the answer is: ' + answer1)
    break
else:
   print("Correct! Here's the next question")
于 2012-12-19T22:59:25.970 回答