2022-10-11 19:27:51 +00:00
|
|
|
# A teenager is a person between 13 and 19 years old, inclusive.
|
|
|
|
# A child is under 13. A grown-up is 20 or more.
|
|
|
|
# This program outputs the age category for a given input age.
|
|
|
|
# It has a semantic error. Can you find it?
|
|
|
|
# Which values of age produce the output "grown-up"?
|
|
|
|
# Correct the error.
|
|
|
|
# Can you simplify the code to avoid redundant conditions?
|
|
|
|
|
|
|
|
age = int(input("Age? "))
|
|
|
|
|
|
|
|
if age < 0:
|
|
|
|
print("ERROR: invalid age!")
|
2023-05-16 20:00:37 +00:00
|
|
|
exit(1) # this terminates the program
|
2022-10-11 19:27:51 +00:00
|
|
|
|
|
|
|
print("Age:", age)
|
|
|
|
|
2023-05-16 20:00:37 +00:00
|
|
|
if age < 13:
|
2022-10-11 19:27:51 +00:00
|
|
|
cat = "child"
|
|
|
|
elif 13 < age < 20:
|
|
|
|
cat = "teenager"
|
|
|
|
else:
|
|
|
|
cat = "grown-up"
|
|
|
|
|
2023-05-16 20:00:37 +00:00
|
|
|
print("Category: ", cat)
|