How old will I be in 2099?
john'sjust turned four and he wants to know how old he will be in various years inthe future such as 2090 or 3044. His parents can't keep up calculating this sothey've begged you to help them out by writing a programme that can answerPhilip's endless questions.
Your task is towrite a function that takes two parameters: the year of birth and the year tocount years in relation to. As Philip is getting more courious every day he maysoon want to know how many years it was until he would be born, so yourfunction needs to work with both dates in the future and in the past.
Provide output inthis format: For dates in the future: "You are ... year(s) old." Fordates in the past: "You will be born in ... year(s)." If the year ofbirth equals the year requested return: "You were born this veryyear!"
"..." areto be replaced by the number, followed and proceeded by a single space. Mindthat you need to account for both "year" and "years",depending on the result.
code1:
def calculate_age(year_of_birth,current_year):
result1 = current_year - year_of_birth
result2 = year_of_birth - current_year
if result1 >1:
return 'You are %d years old.' %result1
elif result1 ==1:
return 'You are 1 year old.'
elif result1 ==0:
return 'You were born this very year!'
elif result2 == 1:
return 'You will be born in 1 year.'
elif result2 >1 :
return'You will be born in %d years.' % result2
code2:
def calculate_age(year_of_birth, current_year):
diff =abs(current_year - year_of_birth)
plural ='' if diff == 1 else 's'
if year_of_birth < current_year:
return 'You are {} year{} old.'.format(diff, plural)
elif year_of_birth > current_year:
return 'You will be born in {} year{}.'.format(diff, plural)
return 'You were born this very year!’