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

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

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.
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?
The following answer may explain some of the more technical details concerning variables. How do I pass a variable by reference?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.