简体   繁体   English

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

[英]Python Using variable from one function in another

In one function I get three pieces of information and write them to a text file.在一个函数中,我获得了三条信息并将它们写入一个文本文件。 In another function I write over one of the pieces of information (Code).在另一个函数中,我写了一条信息(代码)。

The variables FirstName, SecondName, Code from function1 are not known by function2 - how do I solve this?变量FirstName, SecondName, Codefunction1没有已知function2 -我该如何解决这个问题? / Pass them from one function to another? / 将它们从一个函数传递到另一个函数?

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() 

How about return the values of the function and assign them to variables like this如何return函数的值并将它们分配给这样的变量

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

Then you can assign them and use them in the rest of your code然后您可以分配它们并在其余代码中使用它们

FirstName, SecondName, Code = fucntion1()

You can even now pass those into fucntion2() like this您现在甚至可以像这样将它们传递给fucntion2()

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

And then call function2 like this然后像这样调用function2

function2(FirstName, SecondName, Code)

I would suggest using generic names in your function definitions and also using snake_case over camel_case我建议在您的函数定义中使用通用名称,并在camel_case 上使用snake_case

Here is how I would revise the entire thing :这是我将如何修改整个事情:

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()

Again not sure what the overall goal is, but this will help you with some issues that are preventing you from getting there.再次不确定总体目标是什么,但这将帮助您解决一些阻碍您实现目标的问题。 There is missing information here, menu has no definition.这里缺少信息, menu没有定义。

Variables defined inside a function are considered by python to be Local variable .在函数内定义的变量被 python 认为是Local variable As opposed to other programming languages, Python does not require a local variable to explicitly being declared so, but rather uses the surrounding context to determine whether a variable is global or local.与其他编程语言不同,Python 不需要显式声明局部变量,而是使用周围的上下文来确定变量是全局变量还是局部变量。

If you want to use the three variables first_name , second_name , code for both functions, you have two options:如果要为这两个函数使用三个变量first_namesecond_namecode ,则有两个选择:

Option 1 :选项 1

Move the three variables outside of the function definition, so that they become global.将三个变量移到函数定义之外,使它们成为全局变量。

Option 2 :选项2

Pass the variables from one function to the other.将变量从一个函数传递到另一个函数。

As far as the specific program you are writing, I would go for the first.至于您正在编写的特定程序,我会选择第一个。 I have edited your code accordingly, please read the comments for further indications on how to improve your writing.我已经相应地编辑了您的代码,请阅读评论以获取有关如何改进写作的进一步说明。

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