9

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
Kara
  • 6,115
  • 16
  • 50
  • 57
micma
  • 223
  • 2
  • 4
  • 16

1 Answers1

11

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
Holy Mackerel
  • 3,259
  • 1
  • 25
  • 41
  • Thank you so much! I have one more question though. I have a def inside a def. How do I access that. If I used get_email_address() it would open up the window asking me for my email but I already entered it in so I made a def just containing the return Email. but now that gives me a def inside a def so how would I access it? Thanks for your help! – micma Dec 25 '13 at 04:11
  • Sorry I'm not sure what you mean - but I would suggest perhaps not using nested functions. – Holy Mackerel Dec 25 '13 at 04:19