Python缩进之谜 [英] Python indentation mystery

查看:67
本文介绍了Python缩进之谜的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么会出现以下错误?最后一个print语句不应成为while循环的一部分.

Why am I getting the following error? The last print statement should not be a part of the while loop.

>>> while n>= 0:
...     n = n-1
...     print(n)
... print ("TO A!!")
  File "<stdin>", line 4
    print ("TO A!!")
        ^
SyntaxError: invalid syntax

推荐答案

while循环后,您需要按 enter 退出循环

You need to press enter after your while loop to exit from the loop

>>> n = 3
>>> while n>=0:
...     n = n-1
...     print (n)
...                         # Press enter here
2
1
0
-1
>>> print ("To A!!")
To A!!

注意:-...表示您仍在while块中

Note:- ... implies that you are still in the while block

这篇关于Python缩进之谜的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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