Dec-01-2017, 07:54 AM
First of all - let see why your code does not do what you expect.
When you have multiple conditions with
In your example -
With
That said your code could be simplified to
When you have multiple conditions with
and
all of them need to evaluate to True
in order the whole expression to evaluate to True
.In your example -
age == 13
evaluates to False
, thus the entire expression age == 13 and age < 20
evaluates to False
With
or
at least one of the conditions need to evaluate to True
and the whole expression will evaluate to True
as well. This is why it works when you replace and
with or
.That said your code could be simplified to
age = 16 if age < 2: print('You are a baby!') elif age < 4: print('You are a toddler') elif age < 13: print('You are a kid') elif age < 20: print('You are a teenager') elif age < 65: print('You are a adult') else: print('You are a elder')