How do you carry variables from multiple functions to another

Hello,

   I am trying to create a program that has multiple while true statements, each of which are in different functions and have different variables in them. The problem I am, having is at the end of the program I need to be able to call these variables from their functions into another one in order to multiply them. I can't seem to figure out the pieces of code I need to do this. The variables I am trying to call are DAY from the Day() function. YEAR from the Year() function and Month from the bottom while Truew loop. i am trying to call these functions into the Magic() function. Please help. Thank you
def Day():
   while True:
        D = range(1, 32)
        print()
        DAY = int(input("Please enter a two digit day. "))
        if DAY not in (D):
            print()
            print("Thats an invalid number. ")
        else:
            if  DAY in range(1,32):
                Year()

def Year():
    while True:
        Y = range(100)
        print()
        YEAR = int(input("Please enter a two digit year. "))
        if YEAR not in (Y):
            print()
            print("Thats an invalid number. ")
        else:
            if YEAR in range(100):
                print("yep")
                Magic()

while True:
    M = range(1,13)
    print()
    Month = int(input("Please enter a month 1 through 12. "))
    if Month not in (M):
        print()
        print("That is not a two digit month")
    else:
        if Month in range(1,13):
            Day()


def Magic():
    MAGIC = DAY * Month

Call each function, in turn, and have said function return some value. Then use the returned values when you call Magic(d, m), having done def Magic(DAY, Month): for example.

You’ll need to modify your code some to have that work, but it’s doable.

As a basic working example:

def reply(name):
    answer = f"Hello {name}. How are you today?"
    return answer


name = input("What is your name? ")
the_reply = reply(name)
print(the_reply)

Edit: change code for clarity

When posting online about programming, try to avoid using preformatted blocks for prose.

If you have a lot of text in them, some will end up hidden because of the lack of word wrap.

They’re great for code though, like how you posted your code example.

1 Like