[英]Calling a variable from one function to another function in Python
I'm writing a program to send an email through python. 我正在编写一个程序来通过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?
我有不同的def,它包含包含电子邮件用户名和电子邮件密码等的变量。然后我有另一个def实际发送电子邮件,但它需要包含电子邮件用户名和密码等的变量。我怎么能打电话给来自不同def的变量? Sorry for the weird wording.
抱歉这个怪异的措辞。 I don't know how else to say it :P Thanks!
我不知道怎么说:P谢谢!
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
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. 您需要在辅助函数中返回值,并从主
send_email()
函数调用这些函数,并将返回的值赋给变量。 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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.