0

I tried writing

age = 22
message = "Eligible" if age>=22 else message = "not eligible"
print(message)

The above code is failing with the error

SyntaxError: cannot assign to conditional expression

Why doesn't this work?

Brian61354270
  • 8,690
  • 4
  • 21
  • 43
Babji
  • 11
  • 1
    The ternary operator operates on _expressions_, not arbitrary statements like assignments. You want to write `message = "Eligible" if age>=22 else "not eligible"`. – Brian61354270 Jun 10 '21 at 14:13

1 Answers1

1

You only need to get rid off the message = after the else statement.

message = "Eligible" if age>=22 else "not eligible"

Ternary operators are structed like:

variable = [value on_true] if [expression] else [value on_false]  
Matheus Delazeri
  • 425
  • 2
  • 15