18th Dec 2023, 9:03 PM
Rebecca Harris
Rebecca Harris - avatar
4 odpowiedzi
+ 4
Hi, Rebecca Harris ! Your code seems to work well!
18th Dec 2023, 9:14 PM
Per Bratthammar
Per Bratthammar - avatar
+ 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")
19th Dec 2023, 8:00 AM
Rain
Rain - avatar
+ 2
your code works very well !!!
18th Dec 2023, 10:24 PM
Samar Trigui
Samar Trigui - avatar
+ 1
Rain Oh, good catch about negative age number, haven't thought about it! Thank you
19th Dec 2023, 6:14 PM
Rebecca Harris
Rebecca Harris - avatar