Python 3.x 有个错误,但我不知道如何解决
缩进错误:应为缩进块Python 3.x 有个错误,但我不知道如何解决,python-3.x,Python 3.x,缩进错误:应为缩进块 import time def countdown(n) : while n > 0: #here Error print (n) n = n - 1 if n ==0: print('BLAST OFF!') countdown(50) 编程的最佳实践是学习如何使用谷歌。每当你遇到错误,就在网上搜索。 下面的代码片段适用于您 import time def countdown(n) : while n > 0: #here Error
import time
def countdown(n) :
while n > 0: #here Error
print (n)
n = n - 1
if n ==0:
print('BLAST OFF!')
countdown(50)
编程的最佳实践是学习如何使用谷歌。每当你遇到错误,就在网上搜索。 下面的代码片段适用于您
import time
def countdown(n) :
while n > 0: #here Error
print (n)
n = n - 1
if n ==0:
print('BLAST OFF!')
countdown(50)
Python没有花括号。所以要标记所有需要识别的内容。尝试上面的代码,它应该可以工作。您需要查看python文档: 在Python中,缩进不仅仅是修饰,而是定义块和块范围的语法的一部分。在您的例子中,您显然想要定义一个名为countdown的函数。这个函数是一个必须缩进的块。这就是你的错误信息所说的 您的代码应该如下所示:
import time
def countdown(n) :
while n > 0: #here you need to indent
print (n)
n = n - 1
if n ==0: #this may or may not be within the while loop as it's entirely redundant
print('BLAST OFF!') #and the statements here is also a block
countdown(50)
看看这些文件就行了。例如。这里
祝你好运 必须缩进函数体和while块体。请阅读Python教程。对不起,我忘了它应该是这样的:if n==0:printflast OFF倒计时50 while的主体也应该缩进。@juzraai:抓得好