0
Exercise check
Guys, did I do this right? https://sololearn.com/compiler-playground/cPr49Ct1VN36/?ref=app
4 Réponses
+ 4
Hi, Rebecca Harris !
Your code seems to work well!
+ 3
Rebecca Harris ,
When processing input, you probably want to prepare for bad values too. Negative ages would fall into your elderly range, so you might want to create an error range to catch them instead.
Otherwise, it's good. 👍
By the way if you want to optimize, you can eliminate half of the comparisons.
Since if elif else stops at the first True clause, and your ranges have no overlaps or gaps, and you're testing the ranges in ascending order, you don't have to check the lower bound if a previous range would have caught it.
For example.
age = int(input())
if age < 0:
print("Error")
elif age < 2:
print("Baby")
elif age < 4:
print("Kid")
elif age < 13:
print("Child")
elif age < 20:
print("Teenager")
elif age < 65:
print("Adult")
else:
print("Elderly")
+ 2
your code works very well !!!
+ 1
Rain
Oh, good catch about negative age number, haven't thought about it! Thank you