Jan-01-2021, 04:41 PM
Hi,
An external txt file has 2 numbers in it (default). I'm asking a user to input 2 new numbers in my script. These then get written to the text file. The code then reads the default 2 numbers which were already in there, overwrites them, and then prints new numbers in there.
The code is supposed to go until the while loop is false. At the end of the function
There's likely some issue with calling
Where am I going wrong?
An external txt file has 2 numbers in it (default). I'm asking a user to input 2 new numbers in my script. These then get written to the text file. The code then reads the default 2 numbers which were already in there, overwrites them, and then prints new numbers in there.
The code is supposed to go until the while loop is false. At the end of the function
def file_access(a,b):, I have:
choice = input("Press any key to continue, or type 'n' to end: ") if choice != -1: print() else: this_var = choice return choiceI'm trying to make it if the user enters anything but 1, then
file_access(a,b)will return this entry to the while loop, and thus ends it. However, even pressing 1 ends the code.
There's likely some issue with calling
file_access(num1, num2)inside
input_nums(). I also tried setting
this_varto some terminal character at the end, and I thought that would work since it's a global variable, but that didn't work either.
Where am I going wrong?
def input_nums(): num1 = input("enter num1: ") #data type of input MUST be a string, in order to write to a file num2 = input("enter num2: ") file_access(num1, num2) #this passes parameters, num1 and num2 into arguements of func 'file access' # this is VERY impt, as it allows you to return 2 vars into another function def file_access(a,b): #read what's in file currently, print results print("\nPreviously in file:") file = open("demo.txt", 'r') # (which file to open, mode) Mode means what you're doing to the file (e.g. write or read) # 'w' = write to file, 'r' = read file content = file.read() #after reading file, it must store the content, e.g. in a variable, 'content' print(content) # print what was read file.close() # after opening a file, you always have to close it #write to file print("\nNew numbers in file:") file = open("demo.txt", "w") file.write("Number 1 entered = ") # writes text to file file.write(a) # writes text to file, MUST pass a string file.write("\n") file.write("Number 2 entered = ") # writes text to file file.write(b) # writes text to file file.write("\n") file.close() # closes file #read file file = open("demo.txt", "r") # opens file, tells program it will write to it content = file.read() # reads file, returns info to var 'content' print(content) # prints var content file.close() choice = input("Press '1' to continue, or any other key to end: ") if choice == 1: print() #do nothing, just skip a line else: this_var = choice # this will end code return choice this_var = 1 # default condition, so while loop will always run while this_var == 1: this_var = input_nums() print("this_var = ",this_var)