Python在另一个函数中使用变量

时间:2018-09-15 16:47:28

标签: python

在一个函数中,我获得了三段信息,并将其写入文本文件。 在另一个函数中,我覆盖了其中一条信息(代码)。

FirstName, SecondName, Code不知道function1中的变量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() 

2 个答案:

答案 0 :(得分:0)

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没有定义。

答案 1 :(得分:0)

在函数内部定义的变量被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()