uaveiro-leci/1ano/1semestre/fp/aula02/age.py

25 lines
613 B
Python
Raw Normal View History

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!")
exit(1) # this terminates the program
2022-10-11 19:27:51 +00:00
print("Age:", age)
if age < 13:
2022-10-11 19:27:51 +00:00
cat = "child"
2023-05-17 09:30:19 +00:00
elif age < 20:
2022-10-11 19:27:51 +00:00
cat = "teenager"
else:
cat = "grown-up"
print("Category: ", cat)