Q2
unknown
python
4 years ago
1.2 kB
5
Indexable
# Write a function in Python programming language to take a user's date of birth # and calculate the approximate number of seconds they have been alive. # You must take into account leap years in your calculation. # The program must re-run after each round is complete. # Make sure comments are included in your code demonstrating logical steps. from datetime import date # Built in library for time handling ### -- Basic logic: -- ### birthsday_date = date(1993, 7, 29) # date format pattern (yyyy, m, d) current_date = date.today() # today's date # Difference between birth and current date: delta = current_date - birthsday_date # Getting seconds from days: seconds_alive = delta.days * 24 * 3600 print(seconds_alive) print(delta.days) print(delta.seconds) # TODO: ### Transform the code to take a user's date of birth. # # Hint: # Ask for date in format yyyy-mm-dd and use split function # to put in in a list (https://www.w3schools.com/python/ref_string_split.asp) # # You will have 3 separate elements (strings) in a list and then you can create variables # year, month, day by accessing a certain element ### Use a loop to allow user to re-run code
Editor is loading...