突破python的循环
>>> while True:
... line = input('> ')
... if line == 'done' :
... break
... print(line)
... print( 'Done!' )
File "<stdin>", line 6
print( 'Done!' )
^^^^^
SyntaxError: invalid syntax**
对于Python 3,此代码应该给我一个“完成!”当达到“完成”中断陈述时,响应。但是,我不断获得最后一行的语法错误。我试图通过多种方式做到这一点,并且可以在没有上次声明的情况下获得休息订单来工作,但是,在完成“ break”语句的情况下,它永远不会在最后一个语句“打印('print('doad!')中)。抱歉,这只是简单的代码,但我似乎无法使其工作。 PS,我将其修复为使其正如我应该写的那样使它做到,虽然True语句不应该是两次(我在此处复制它的错误)以及第五行上的额外空间。我尝试了
>>> while True:
... line = input('> ')
... if line == 'done' :
... break
... print(line)
... print( 'Done!' )
File "<stdin>", line 6
print( 'Done!' )
^^^^^
SyntaxError: invalid syntax**
For python 3 this code should give me a "Done!" response when the break statement "done" is reached. However, I keep getting a syntax error for the last line. I have tried to do it multiple ways and I can get the break order to work without the last statement, however, it never works where I include the last statement "print('Done!')" with the break statement done. I apologize, this is just simple code but I can't seem to get it to work. Ps, I fixed it to make it as I am supposed to be writing it, the while True statement was not supposed to be twice (my error in copying it here) along with the extra space on the fifth line. and I have tried
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这不是语言,而是您的IDE。它只期望单个语句和下属的行,即缩进。一旦您到达缩进的末尾,它会期望一条空白,并且在您尝试发表其他声明时不知道该怎么办。
如果同一序列是文件的一部分,它将完全按照您的期望。如果将所有功能都放入功能中,则整个功能主体将被缩进,并且也可以工作。
It's not the language, it's your IDE. It is only expecting a single statement and the lines that are subordinate to it, i.e. indented. Once you've reached the end of the indenting it expects a blank line and doesn't know what to do when you try to give another statement.
If that same sequence were part of a file it would do exactly what you expect. If you put it all into a function, the whole function body will be indented and that would work too.