How old are you
Enter your day, month and year of birth and the program works out your age; on your birthday it wishes you well and plays a tune. Teaches working with today's date and conditions with and and or.
Open in Tokbit Level: Intermediate. Free in the browser, nothing to install, no account needed.
Run the example in Tokbit and step through it: before each step say what will happen and check it in the Variables panel. Then change it your way.
Flowchart
Python program
The same program as Python code. In Tokbit you see it when you switch from Diagram to Python above the flowchart.
"""Works out your age from your date of birth and today's date. Teaches year(), month(), day() and a compound condition with and and or."""
# Tokbit screen functions, not in plain Python:
from tokbit import year, month, day, note
birth_day: int
birth_month: int
birth_year: int
# Asks again until the day makes sense.
while True:
birth_day = int(input("Day of birth (1–31):"))
if birth_day < 1 or birth_day > 31:
print("The day must be from 1 to 31.")
if not (birth_day < 1 or birth_day > 31):
break
while True:
birth_month = int(input("Month of birth (1–12):"))
if birth_month < 1 or birth_month > 12:
print("The month must be from 1 to 12.")
if not (birth_month < 1 or birth_month > 12):
break
while True:
birth_year = int(input("Year of birth:"))
if birth_year > year():
print("The year can't be in the future.")
if not (birth_year > year()):
break
# Age by the calendar year; year() is this year.
age: int = year() - birth_year
# No birthday yet this year: one year less.
if month() < birth_month or (month() == birth_month and day() < birth_day):
age = age - 1
# In Czech 1 rok, 2–4 roky, otherwise let; in English year for 1, otherwise years.
if age == 1:
print("You are", age, "year old.")
else:
if age >= 2 and age <= 4:
print("You are", age, "years old.")
else:
print("You are", age, "years old.")
# Birthday today: a wish and a tune.
if month() == birth_month and day() == birth_day:
print("Happy birthday!")
note("C5", 200)
note("E5", 200)
note("G5", 200)
note("C6", 400)
Follow-up tasks
Tasks in which you change the example (with a model solution):