Python中断功能不会在true时结束 [英] Python break function doesn't end while true

查看:85
本文介绍了Python中断功能不会在true时结束的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么中断不会在while true时结束并返回到起点?

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

我是python的新手,所以我认为这只是对术语的简单误用.

I'm pretty new to python so I assume it's just a simple misuse of the terms.

推荐答案

break 存在整个 while 循环.

continue 是您要的内容,返回到下一个 while 迭代.

continue is what you're looking for, returning to the next while iteration.

您可以在官方文档中了解有关控制流工具, break continue 的更多信息:

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)

这篇关于Python中断功能不会在true时结束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆