Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling a variable from one function to another function in Python [duplicate]

Tags:

python

I'm writing a program to send an email through python. I have different def's that hold the variables that contain the email username and email password etc.. Then I have another def that actual sends the email, but it needs the variable that contains the email username and password etc.. How can I call the variables from the different def's? Sorry for the weird wording. I don't know how else to say it :P Thanks!

def get_email_address():
    #the code to open up a window that gets the email address
    Email = input
def get_email_username():
    #the code to open up a window that gets email username
    Email_Username = input

    #same for the email recipient and email password

def send_email():
    #here I need to pull all the variables from the other def's
    #code to send email
like image 974
micma Avatar asked Dec 25 '13 03:12

micma


People also ask

Can you call a variable from one function to another in Python?

The variable can be assigned to the function object inside the function body. So the variable exists only after the function has been called. Once the function has been called, the variable will be associated with the function object.

How do you use a variable from one function to another?

You can use the function to set the value of a variable outside the context of the function, then you can use it as an argument in another function. You can use the function to set the value of a variable outside the context of the function, then you can use it as an argument in another function.

How do you repeat a variable in Python?

To repeat a string in Python, We use the asterisk operator ” * ” The asterisk. is used to repeat a string n (number) of times. Which is given by the integer value ” n ” and creates a new string value. It uses two parameters for an operation: the integer value and the other is String.


1 Answers

You would need to return values in your helper functions and call those functions from your main send_email() function, assigning the returned values to variables. Something like this:

def get_email_address():
    #the code to open up a window that gets the email address
    Email = input
    return Email

def get_email_username():
    #the code to open up a window that gets email username
    Email_Username = input
    return Email_Username

#same for the email recipient and email password

def send_email():
    # variables from the other def's
    email_address = get_email_address()
    email_username = get_email_username()

    #code to send email
like image 193
Holy Mackerel Avatar answered Oct 25 '22 22:10

Holy Mackerel