if/elif question, it works, but the answer is wrong

Viewed 38
age = 20

if age < 2:
    
    print('the person is a baby')

elif age >= 2:
    
    print('toddler')

elif age >= 4:
    
    print('kid')

elif age >= 13:

    print('teenager')

elif age >= 20:
    
    print('adult')

elif age >= 65:
    
    print('elder')

it works, but when age is more and 2, the answer is always toddler. I do not know what happened.

1 Answers

Ya that's obvious

If one of your elif statement got executed then rest of all elif or else will be ignored

You can specify conserver condition with the elif condition

example::

if age < 2:
   print('the person is a baby')

elif age >= 2 and age < 4:
   print('toddler')

elif age >= 4 and age < 13:
   print('kid')

else:
   print('Out')

Likewise you can limit the conditions.

Related