0

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
octopusgrabbus
  • 10,555
  • 15
  • 68
  • 131
Nancy
  • 179
  • 1
  • 5
  • 15

5 Answers5

9

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":
Tim Pietzcker
  • 328,213
  • 58
  • 503
  • 561
  • OMG, just like that!! Thank you, I tried so much times.... So stupid. Thank you, Tim. It helps. – Nancy Jun 25 '12 at 09:41
  • Tim pointed out the error in your code, also please ensure you do not use 'is' keyword for string comparison. Reason is here: http://stackoverflow.com/questions/2987958/how-is-the-is-keyword-implemented-in-python – 18bytes Jun 25 '12 at 12:00
  • Thank, Devsundar. I'll pay attention on that. :-) Have a good day! – Nancy Jun 26 '12 at 01:38
5

Many Python constructs, like if, while, and for, require a terminating colon :, and the lines following must be indented all at the same level.

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:

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"
octopusgrabbus
  • 10,555
  • 15
  • 68
  • 131
  • For whoever marked this answer down, if you would say what you did not like about my answer, I would edit and fix it up. – octopusgrabbus Jun 26 '12 at 12:20
  • 1
    I guess it may have to do with the fact that your code still throws a `SyntaxError`. Perhaps also because you're mixing Python 2 and Python 3 syntax (although that's not the cause of the error). – Tim Pietzcker Jun 26 '12 at 19:22
  • It was meant as an example only, so I'll go edit not to throw an error. Thanks. – octopusgrabbus Jun 26 '12 at 19:29
  • ```if “year” == 2019: genre = 33``` is still failing for me :( – Freedo Jul 12 '19 at 07:01
  • "year" is a string. It will always fail being compared to 2019. I'm surprised you're not getting an error trying to compare the two. if year's value is numeric, then if year == 2019: would be a legitimate comparison. – octopusgrabbus Jul 12 '19 at 14:36
2

You need a colon at the end of the line,this way if result == "PASS":

octopusgrabbus
  • 10,555
  • 15
  • 68
  • 131
Andrew Miller
  • 143
  • 1
  • 9
2

You missed the colon operator after the if statement.

result = getString(argument_x)
print result # it returns "PASS"
if result == "PASS":
    print 'something'
Matthew Barlowe
  • 2,229
  • 1
  • 14
  • 24
dsqad
  • 21
  • 1
  • 1
    Your answers are more likely to be found useful and upvoted if you format the code properly so it can read easily. You can find help on that [here](https://meta.stackexchange.com/questions/22186/how-do-i-format-my-code-blocks) – Matthew Barlowe May 14 '20 at 17:10
1

You missed the colon operator after the if statement.

result = getString(argument_x)
print result # it returns "PASS"
if result == "PASS":
   print 'something'
octopusgrabbus
  • 10,555
  • 15
  • 68
  • 131
SkariaArun
  • 219
  • 1
  • 13