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.
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.
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.