簡體   English   中英

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

[英]Python Using variable from one function in another

在一個函數中,我獲得了三條信息並將它們寫入一個文本文件。 在另一個函數中,我寫了一條信息(代碼)。

變量FirstName, SecondName, Codefunction1沒有已知function2 -我該如何解決這個問題? / 將它們從一個函數傳遞到另一個函數?

def function1():
    FirstName = input("Enter First Name")
    SecondName = input("Enter Surname")
    Code = input("Enter Code")

AllDetails = (GuestFirstName, GuestSecondName, Code)
f = open("AllDetails.txt","w") 
f.write(str(AllDetails))    
f.close()

menu()

def function2():           
    Newcode = input ("Enter if new code needed")
    if Newcode == "Y":
        Code = "****"
        AllDetails = (FirstName, SecondName, Code)
        f = open("AllDetails.txt","w") 
        f.write(str(AllDetails))
        f.close()

menu() 

如何return函數的值並將它們分配給這樣的變量

def function1():
    FirstName = input("Enter First Name")
    SecondName = input("Enter Surname")
    Code = input("Enter Code")
    return FirstName, SecondName, Code

然后您可以分配它們並在其余代碼中使用它們

FirstName, SecondName, Code = fucntion1()

您現在甚至可以像這樣將它們傳遞給fucntion2()

def function2(FirstName, SecondName, Code);
    ....

然后像這樣調用function2

function2(FirstName, SecondName, Code)

我建議在您的函數定義中使用通用名稱,並在camel_case 上使用snake_case

這是我將如何修改整個事情:

def get_info():
    first = input("Enter First Name")
    second = input("Enter Surname")
    user_code = input("Enter Code")
    return  first, second, user_code

def write_info(f_name, s_name, code_in):
    new_code  = input ("Enter if new code needed")
    if new_code == "Y":
        code_in = "****"
        all_details = (f_name, s_name, code_in)
        f = open("AllDetails.txt","w")
        f.write(str(all_details))
        f.close()
    else:
        pass

first_name, second_name, code = get_info()
write_info(first_name, second_name, code)

all_details = (guest_first, guest_second, code)
f = open("AllDetails.txt","w") 
f.write(str(all_details))
f.close()

menu()

再次不確定總體目標是什么,但這將幫助您解決一些阻礙您實現目標的問題。 這里缺少信息, menu沒有定義。

在函數內定義的變量被 python 認為是Local variable 與其他編程語言不同,Python 不需要顯式聲明局部變量,而是使用周圍的上下文來確定變量是全局變量還是局部變量。

如果要為這兩個函數使用三個變量first_namesecond_namecode ,則有兩個選擇:

選項 1

將三個變量移到函數定義之外,使它們成為全局變量。

選項2

將變量從一個函數傳遞到另一個函數。

至於您正在編寫的特定程序,我會選擇第一個。 我已經相應地編輯了您的代碼,請閱讀評論以獲取有關如何改進寫作的進一步說明。

first_name = input("Enter First Name: ") # Note the lowercase names for the 
second_name = input("Enter Surname: ")   # variables. This is not obligatory,
code = input("Enter Code: ")             # but it is good practice.

def function1():

    all_details = first_name+'_'+second_name # the + replaces the tuple,
                                   # because filename that you want to create must be
                                   # necessarily a string, not a tuple
    f = open(all_details+'.txt',"w") 
    f.write(first_name+','+second_name+','+code)    
    f.close()

# menu() # I presume menu() is the function that does something magical,
         # and either calls function1() or function2(). You don't want to call it twice probably

def function2():

    new_code_needed = input ("Enter \'Y\' if new code needed: ")
    if new_code_needed == "Y":
        code = "****"
        all_details = first_name+'_'+second_name # Same as above
        f = open(all_details+".txt","w") 
        f.write(first_name+','+second_name+','+code)
        f.close()

menu() 

暫無
暫無

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

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