The What's My Age Again (in Hours)? Program
The following program is named after the pop-punk classic by Blink 182 - What's My Age Again off the album Enema of the State.
The program asks the user for the hour, day, month, and year of their birth.
It then outputs the person's age in hours.
Here is a plan for our program.
In pseudocode:
Ask the user for the hour, day, month and, year of birth
Compute the difference between the current date and time and the user's birth
Convert the difference into hours
Print out the user's age in hours
Please try to understand how this program works and match it with the pseudocode above.
Copy and paste this code into a new file to play around with it.
1. The Complete Program
import datetime
hour = int(input("What hour (24hr) were you born?\n"))
day = int(input("What day (number) of the month were you born?\n"))
month = int(input("What number month were you born?\n"))
year = int(input("What year were you born?\n"))
# create a new datetime object with user input
birth_datetime = datetime.datetime(year, month, day, hour)
# get current datetime
current_datetime = datetime.datetime.now()
# compute the difference
diff = current_datetime - birth_datetime
# convert days (*24) and seconds (/3600) to hours and sum
hours = diff.days*24 + diff.seconds/3600
print(f"Your age in hours is {round(hours)}.")
How would you estimate that the output is actually correct?
You should try and do a calculation to convince yourself that this works properly.