-1

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? / 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() 
vash_the_stampede
  • 4,590
  • 1
  • 8
  • 20

2 Answers2

0

How about return the values of the function and assign them to variables like this

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

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

And then call function2 like this

function2(FirstName, SecondName, Code)

I would suggest using generic names in your function definitions and also using snake_case over camel_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.

vash_the_stampede
  • 4,590
  • 1
  • 8
  • 20
  • I had already tried this. But when I try and return the values, the code just stops running at that point. It does not return to the main menu (from which I get to the second function. – John Collins Sep 15 '18 at 17:01
  • Yes I know and I understand, not being rude but I would step back a few steps before you get into attempting writing and working with files and really drill in how to use functions, I would recommend Python Crash Course and Automate the Boring Stuff, and of course Stack Overflow has tons of information avaiblable, best of luck! – vash_the_stampede Sep 15 '18 at 17:07
0

Variables defined inside a function are considered by python to be 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.

If you want to use the three variables first_name,second_name, code for both functions, you have two options:

Option 1:

Move the three variables outside of the function definition, so that they become global.

Option 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() 
Daneel R.
  • 527
  • 3
  • 9