0

I am new to Python and currently practicing online resources. In this question, I have to take user input for today's date, birth date -> calculate the total days for each and then tell the difference between the days left in birthday.

The challenge is to split this into 3 functions and solve it. I have split it into 3 functions this way:

  1. to get actual date days
  2. to get birth date days
  3. to calculate the difference

Can someone please help me how to reference the return values of actualDates() and birthDay() in the diff() function, and return the actual difference?

Also, if someone can help me on how to make the monthDays() list global, that would be helpful as well.

def actualDates():
    actual_months =[]
    #monthDays = monthDays()
    monthDays = [31, 28, 31, 30, 31, 30,31, 31, 30, 31, 30, 31]

    today_months= int(input("What is the month (1-12)?"))
    if today_months in ( 1, 3,5, 7, 8,10, 12):
        today_days= int(input("What is the day   (1-30)?"))
    else:
        today_days= int(input("What is the day   (1-31)?"))
    for i in range(0, today_months-1):
         actual_months.append(monthDays[i])
    if today_months <= 1:
        today = today_days
    else:
        today = sum(actual_months) + today_days
    return today


def birthDay():
    actual_months =[]
    monthDays = [31, 28, 31, 30, 31, 30,31, 31, 30, 31, 30, 31]
    today_days= int(input("Please enter your birthday:"))
    
    today_months= int(input("What is the month (1-12)?"))
    if today_months in ( 1, 3,5, 7, 8,10, 12):
        today_days= int(input("What is the day   (1-30)?"))
    else:
        today_days= int(input("What is the day   (1-31)?"))
    for i in range(0, today_months-1):
         actual_months.append(monthDays[i])
    if today_months <= 1:
        birth = today_days
    else:
        birth = sum(actual_months) + today_days
    return birth


def diff(today , birth):
    pass

diff()
Vedant
  • 21
  • 5

1 Answers1

2

You can pass the dates to the diff() like this:

def diff(today , birth):
    return birth - today

today = actualDates()
birth = birthDay()
diff = diff(today, birth)
print(diff)

You can make the list monthDays global by putting

monthDays = [31, 28, 31, 30, 31, 30,31, 31, 30, 31, 30, 31]

at the top of the code outside of the function. Then inside of any function monthDays is being used, you can declare it global:

def actualDates():
    global monthDays

Also, you may wish to investigate the Python module datetime in the Python documentation. You can calculate the time difference between two dates using datetime.timedelta.

tibafe0123
  • 64
  • 3
  • `global monthDays` isn't needed here. Since `monthDays` is only being read (and not written) in the function, python will look for it in the global namespace. `global` is only needed if you plan to assign `monthDays` in the function. – tdelaney Jun 12 '22 at 04:54
  • That you, that was really helpful! So in this, each of the object we made by calling the function, the return value was stored in that object. Correct? – Vedant Jun 12 '22 at 05:08
  • 1
    The following answer may explain some of the more technical details concerning variables. [How do I pass a variable by reference?](https://stackoverflow.com/questions/986006/how-do-i-pass-a-variable-by-reference) – tibafe0123 Jun 12 '22 at 05:51