Write a function named get_age_on(birthday, other) that accepts two Date objects
ID: 3866220 • Letter: W
Question
Write a function named get_age_on(birthday, other) that accepts two Date objects as parameters: one to represent a person’s birthday, and one to represent an arbitrary date. The function should then return the person’s age on that date as an integer.
Notes:
You can assume that the other parameter will represent a date on or after the birthday date.
It may be helpful to construct a new Date object that represents the person’s birthday in the year of other. That way, you can determine whether the person’s birthday has already passed in the year of other, and use that information to calculate the age.
Example:
Write a function print_birthdays(filename) that accepts a string filename as a parameter. The function should then open the file that corresponds to that filename, read through the file, and print some information derived from that file.
More specifically, the function should assume that the file in question contains information about birthdays in lines of the following format:
In other words, each line of the file contains comma-separated birthday data.
The function should read this file line-by-line, and print the person’s name, birthday, and the day of the week on which the person was born in the following format:
For example, the file birthdays.txt contains the following data:
Therefore, calling print_birthdays with this filename should print the following information:
Notes:
For full-credit, the format of the printed text should exactly match the formatting scheme described above, including spaces and parentheses.
You should process the text file using the line-by-line technique shown in lecture.
For every line of the file, you will need to create a Date object and invoke the appropriate methods on the object to get the information needed.
Originally, the components of the date that you obtain from the file will be in string form. You will need to convert them to integers before you pass them into the Dateconstructor, and you can use the int function for this purpose.
You can get a string representation of a Date object named d using the expression str(d).
Remember that you can concatenate strings together using the string concatenation operator (+). This operator will be helpful when you try to wrap parts of the output in parentheses. For example:
Explanation / Answer
#!usr/python/bin
import time
from datetime import date
def get_age_on(d1 , d2):
diffy = d2.year - d1.year
diffm = d2.month - d1.month
diffd = d2.day - d1.day
if diffm == 0 and diffd > 0:
return diffy
if diffm < 0:
return diffy-1
if diffm > 0:
return diffy
if diffm == 0 and diffd < 0:
return diffy-1
if diffm == 0 and diffd == 0:
return diffy
def print_birthdays(filename):
with open("birthdays.txt","r") as fin:
for line in fin:
name = line.split(',')[0]
if int(line.split(',')[1]) < 10 :
month = "0" + line.split(',')[1]
else:
month = line.split(',')[1]
if int(line.split(',')[2]) < 10 :
day = "0" + line.split(',')[2]
else:
day = line.split(',')[2]
year = line.split(',')[3]
year = year.replace(' ','')
str = year + "-" + month + "-" + day
weekday = time.strftime("%A", time.strptime(str, "%Y-%m-%d"))
print(name + " " +"(" + month + "/" + day + "/" + year + ")" + " " + "(" + weekday + ")")
birthday = input("Enter birthday as year,month,day:")
dt = input("Enter date to calculate the age as year,month,day:")
date1 = date(int(birthday.split(',')[0]), int(birthday.split(',')[1]), int(birthday.split(',')[2]))
date2 = date(int(dt.split(',')[0]), int(dt.split(',')[1]), int(dt.split(',')[2]))
print("Age is:",get_age_on(date1,date2))
filename = input("Enter filename:")
print_birthdays(filename)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.