Home > Software engineering >  How to reference the return values from 2 functions in another function?
How to reference the return values from 2 functions in another function?

Time:06-12

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()

CodePudding user response:

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.

  • Related