简单的print语句不能在python中使用if语句的方法内部工作

2024-05-20 01:52:27 发布

您现在位置:Python中文网/ 问答频道 /正文

我最近开始学习Python代码,一个简单的print语句在过去4天里给我带来了麻烦。在

问题:print语句在if语句的validatePostcode(postcode)方法中不起作用。赋值为200(状态代码),它在没有if语句的情况下打印良好。另外,当我与该API的True(结果值)进行比较时,如果不使用if语句,它就可以正常工作了,为什么在我应用if并尝试比较之后它就不起作用了?在

错误:

  File "./py_script3.py", line 32
    print ("Congrats")
        ^
IndentationError: expected an indented block

^{pr2}$

Tags: 方法代码pyapitrueif状态错误
3条回答

您应该按如下方式缩进print语句:

if Value == 200 :
   print ("Congrats")

你可以阅读更多关于这个here!在

来自https://docs.python.org/2.0/ref/indentation.html

Leading whitespace (spaces and tabs) at the beginning of a logical line is used to compute the indentation level of the line, which in turn is used to determine the grouping of statements.

通过做

if Value == 200:
print ("Congrats")

Python将这两行解释为两组不同的语句。你应该做的是:

^{pr2}$

这段代码(生成错误):

if Value == 200 : 
print ("Congrats")

应该是

^{pr2}$

因为python希望条件后面有一个缩进的块,就像错误消息告诉您的那样

相关问题 更多 >