0

My homework is asking me to: Define a function make_birthday_intro() that takes in two arguments: a name (string), and a birth date (date).

You should utilize your make_introduction() function from Part 1! You may need to calculate a variable to pass into that function call.

Hint: use the relativedelta() function to calculate the person's current age, as well as when they will turn 1 year older. You can get the number of days or years from a relativedelta value (e.g., time_difference) by accessing the .days or .years properties (e.g., time_difference.years).

And the second part

Create a variable my_bday_intro by calling your make_birthday_intro() function and passing in your name (already a variable!) and your birthdate. Print the variable after you create it.

My tutor and I really struggled to work this out together but I believe part of the problem was that we didn't work through part one of the assignment together so he didn't fully understand this part of the assignment which I realized later may be part of why we were getting so stuck, we were missing a variable. I can't figure out where to even start with the second part of the assignment because we got so stuck on the make_birthday_intro portion.

my make_introduction code from part 1 including all code leading up to it

my_name = "Kaitlyn Griffith"
print(my_name)

my_age = 24
print(my_age)

def make_introduction(my_name, my_age):
    return "Hello, my name is, " + my_name + " and I'm " + str(my_age) + " years old."

My attempt at the homework problem

import datetime

def make_birth_intro(name, date_of_birth):
    age = datetime.date.today() - date_of_birth
    print(age)

dateThing = datetime.date(1995, 2, 10)
make_birth_intro(make_introduction, dateThing)

I'm not sure where to start with the second part of the assignment

This function should return a string of the format "Hello, my name is {NAME} and I'm {AGE} years old. In {N} days I'll be {NEW_AGE}" (replacing {NAME}, {AGE}, {N}, and {NEW_AGE} with appropriate values).

Where it should ready "Hello, my name is Kaitlyn and I'm 24 years old. In 274 days I'll be 25"

however, my current output is:

8857 days, 0:00:00

And I am honestly not sure what I should be looking for in the second part

Bill Hileman
  • 2,798
  • 2
  • 17
  • 24
  • `make_birth_intro(make_introduction(my_name, my_age), dateThing)`. A functions arguments need to be passed. The arguments cant be variable references themselves. Either you pass it or use global variables within the function. – Error - Syntactical Remorse May 12 '19 at 23:14

2 Answers2

0

You are very close. To get the age in years, you could take only the year parameters from the dates and subtract them to get the difference in years. To get the days left for the birthday, you could first get the birth date in the current year and then subtract that from the current date to get the difference in days. The complication arises when the birthday has already passed but could possibly be changed with a simple increment in years.

You would roughly do it like this (haven't tested myself):

def make_birth_intro(name, date_of_birth):
    today = datetime.date.today()
    age = today.year - date_of_birth.year
    print(age)
    this_birthday = date_of_birth.replace(year = today.year)
    if(this_birthday < today):
        this_birthday = this_birthday.replace(year=this_birthday.year + 1)
    days_left = this_birthday - today
    print(days_left.days)

razdi
  • 1,388
  • 15
  • 21
0

I didn't read to much into the whole problem statement and as razdi said you are close, I rewrote some stuff.

With this solution you just need DOB not your age.


import datetime

def make_introduction(my_name, birth_info):
    """
    Handles the printing
    """
    return f"Hello, my name is {my_name} and I'm {birth_info[0]} years old, in {birth_info[1]} I'll be {birth_info[0] + 1}."

def make_birth_intro(date_of_birth):
    # // is a floor division to get age in years given days
    today = datetime.date.today()
    age = (today - date_of_birth).days // 365

    next_birthday = datetime.date(today.year, date_of_birth.month, date_of_birth.day)
    if next_birthday < today:
        """
        If we already had a birthday this year then add a year.
        """
        next_birthday = next_birthday.replace(year=today.year + 1) 
    days_till_level_up = (next_birthday - today).days
    return age, days_till_level_up

my_name = "Kaitlyn Griffith"
DOB = datetime.date(1995, 2, 10)

output = make_introduction(my_name, make_birth_intro(DOB))
print(output)

Output:

Hello, my name is Kaitlyn Griffith and I'm 24 years old, in 274 I'll be 25.
  • but now I get a weird output with the second part ```my_bday_intro = make_birthday_intro(my_name, DOB) print(my_bday_intro)``` #output ```8857 days, 0:00:00 None``` – Kaitlyn Griffith May 13 '19 at 00:33
  • `make_birthday_intro` from my example doesn't take two arguments so I don't know where you got that output (nor do I ever print that number)... But 8857 is your age in days.... 8857 / 365 is 24 and some change. – Error - Syntactical Remorse May 13 '19 at 01:23
  • There are two parts to my question above the second part asks me to Create a variable my_bday_intro by calling your make_birthday_intro() function and passing in your name (already a variable!) and your birthdate. Print the variable after you create it. so I try to do this by ```y_bday_intro = make_birthday_intro(my_name, DOB) print(my_bday_intro)``` and that's how I get the weird output – Kaitlyn Griffith May 13 '19 at 01:35
  • Its not a weird out. It is prints the different of `today - DOB`. You need to manually do something with it such as `my_age = results.days // 365`. Copy and paste my code into and IDE add some prints and walk through it to see how it all works. Then you can answer your questions. – Error - Syntactical Remorse May 13 '19 at 01:44