简体   繁体   English

Python“if”语句语法错误

[英]Python "if" statement syntax error

I am confused about the error I am getting.我对我得到的错误感到困惑。

My code is as below:我的代码如下:

result = getString(argument_x)
print result # it returns "PASS"
if result ="PASS"

When I try to launch it, it shows an error for the last line:当我尝试启动它时,它显示最后一行错误:

SyntaxError: invalid syntax

Comparison for equality is done using the == operator (you're using a single = which is for assignments only).相等性比较是使用==运算符完成的(您使用的是单个= ,它仅用于赋值)。 Also, you're missing a colon:此外,您还缺少一个冒号:

if result == "PASS":

Many Python constructs, like if, while, and for, require a terminating colon : , and the lines following must be indented all at the same level.许多 Python 结构,如 if、while 和 for,需要一个终止冒号: ,并且后面的行必须在同一级别缩进。

The indent level is not as important as all statements associated with the conditional must be indented at the same level.缩进级别并不重要,因为与条件关联的所有语句都必须缩进同一级别。

In your case, you were using an if statement:在您的例子中,您使用的是 if 语句:

result = getString(argument_x)
print result # it returns "PASS"
if result == "PASS":
  print("Result equals pass")
#Add any other statements here to be executed as a result
#of result == "PASS"

You need a colon at the end of the line,this way if result == "PASS":你需要在行尾有一个colon ,这样if result == "PASS":

You missed the colon operator after the if statement.您在 if 语句之后错过了冒号运算符。

result = getString(argument_x)
print result # it returns "PASS"
if result == "PASS":
    print 'something'

You missed the colon operator after the if statement.您在 if 语句之后错过了冒号运算符。

result = getString(argument_x)
print result # it returns "PASS"
if result == "PASS":
   print 'something'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM