-2
age = 22
message = "Allowed" if age >= 18 else message = " not allowed"
print (message) 

What's wrong with this python ternary? Every time I try to execute it says can't assign to conditional expression.

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
Imran Abdalla
  • 115
  • 1
  • 10

1 Answers1

-1

You don't need the second message =: message = "Allowed" if age >= 18 else " not allowed" will work.

Essentially, the Python ternary operator works like this:

[result] = [thing if true] if [condition] else [thing if false]

All that's necessary is a condition and the two separate options: you don't need to assign the message variable twice.

Peritract
  • 761
  • 5
  • 13