0

Can this code stub here below be written more like if-else shorthand syntax?

actual = ""
if fail_or_pass == 'FAIL':
    actual = "false"
else:
    actual = "true"

I was trying something like this, but it did not work

actual = ""
acutal = 'false' if fail_or_pass == 'FAIL' else actual = 'true'
JonB
  • 662
  • 1
  • 7
  • 25
  • What you mean is: `actual = 'false' if fail_or_pass=='FAIL' else 'true'` – khelwood Nov 01 '16 at 10:10
  • 1
    `.. if ... else ...` is an expression producing a value. Remove the `actual =` from the right-hand side `else` branch, and correct your variable name (`acutal` is misspelled). `actual = 'false' if fail_or_pass == 'FAIL' else 'true'` – Martijn Pieters Nov 01 '16 at 10:10

1 Answers1

3

The inline if / else is an expression. It will evaluate to the first or third part depending on the second part. The assignment should happen to the left side only, not inside:

acutal = 'false' if fail_or_pass == 'FAIL' else 'true'

Also you don't have to initialize the variable before. And as an additional advise: Use True and False instead of strings to store boolean values, it makes everything easier and more consistent.

actual = fail_or_pass != 'FAIL'
Klaus D.
  • 11,472
  • 3
  • 30
  • 43
  • Good point about the `true / false` strings, but I have to return them as strings unfortunately. – JonB Nov 02 '16 at 20:37