簡體   English   中英

在Python中調用一個函數的變量到另一個函數

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

我正在編寫一個程序來通過python發送電子郵件。 我有不同的def,它包含包含電子郵件用戶名和電子郵件密碼等的變量。然后我有另一個def實際發送電子郵件,但它需要包含電子郵件用戶名和密碼等的變量。我怎么能打電話給來自不同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

您需要在輔助函數中返回值,並從主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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM