Program: Calculating Age
In the hello_world_personal.py program, we added 1 to a variable to identify the person's age next year. Having the computer calculate with numbers is a huge part of programming.
def main():
name = input("Enter your name:")
print("Enter your age:")
age = int(input())
print("Hello", name)
print("Next year you will be", age + 1, "years old.")
main()
Another way we could have written that same program is to modify the value of age, replacing whatever was entered before with the same value + 1. We would write those lines like this.
age = int(input))
age = age + 1
print("Next year you will be", age, "years old.")
This would work, too:
age = int(input())
age_next_year = age + 1
print("Next year you will be", age_next_year, "years old.")
Write a new program calculating_age.py that asks the user to enter what year they were born, and then calculates what age they will be this year based on it currently being the year 2026. If I was born in 2020, then, I would turn 6 years old this year.
Use a line like this in your program to calculate that age.
age_this_year = 2026 - birth_year
If you get stuck, here's a solution:
def main():
name = input("Enter your name: ")
birth_year = int(input("Enter the year you were born (4 digits please): "))
age = 2026 - birth_year
print("Wow", name, "!")
print("This year you will be", age, "years old!")
main()