在Python中从一个函数调用变量到另一个函数

发布于 2021-01-29 17:14:35

我正在编写一个程序来通过python发送电子邮件。我有不同的定义,其中包含包含电子邮件用户名和电子邮件密码等的变量。然后我有另一个定义,实际发送电子邮件,但它需要包含电子邮件用户名和密码等的变量。如何调用来自不同def的变量?对不起,措辞怪异。我不知道该怎么说: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
关注者
0
被浏览
103
1 个回答
  • 面试哥
    面试哥 2021-01-29
    为面试而生,有面试问题,就找面试哥。

    您将需要在辅助函数中返回值并从主send_email()函数中调用这些函数,并将返回的值分配给变量。像这样:

    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
    


知识点
面圈网VIP题库

面圈网VIP题库全新上线,海量真题题库资源。 90大类考试,超10万份考试真题开放下载啦

去下载看看