Python break function doesn't end while true

断了今生、忘了曾经 提交于 2021-02-05 07:46:17

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!