I have code where I import variables from another file. In the code I change the variables, and then at the end I write them into the other file where they came from. The way I run this code is from a parent program. My problem arises when I run the parent program, go into this code, change the variables, back out to the parent program, then go back in. When I do this, the variables go back to what they were before I changed them. I have tried to find when this reversion occurs and I am fairly certain it occurs when I import the variables. I put a print(open("file.py").read) before the import and the variables are fine, but afterwards they are altered. I am really not sure why this is happening, especially because I have another program which does something very very similar and it works fine. I tried adding f.flush() after altering the code and it doesn't work.
The parent program (menu.py):
while True:
print()
print("----------------------------------------------------------")
print("Menu:")
print()
print("Enter 1 to quiz yourself")
print()
print("Enter 2 to alter or look up a pair")
print()
print("Enter 3 to input solving data")
print()
print("Enter . to quit")
while True:
choice = input()
if choice in ["1", "2", "3", "."]:
break
else:
print("Enter 1, 2, 3, or .")
if choice == "1":
print()
print("----------------------------------------------------------")
print("Quiz:")
print()
exec(open("quiz.py").read())
elif choice == "2":
print()
print("----------------------------------------------------------")
print("Pairs:")
print()
exec(open("pairs.py").read())
elif choice == "3":
print()
print("----------------------------------------------------------")
print("Input Data:")
print()
exec(open("input_data.py").read())
elif choice == ".":
break
The main file where my problem is occuring (input_data.py):
print(open("accuracy.py").read())
from accuracy import attempts, DNF, solves, current_streak, longest_streak
print(open("accuracy.py").read())
print("Press . to go back to menu")
while True:
print()
print("Attempts: ", attempts)
print("DNFs: ", DNF)
print("Solves: ", solves)
print("Accuracy: {}".format( 100 * solves / attempts))
print("Current Streak: ", current_streak)
print("Longest Streak: ", longest_streak)
print()
print("Did you solve it?")
foo = input("Yes or No: ").upper()
if foo == "YES":
print(":(")
DNF += 1
attempts += 1
current_streak = 0
elif foo == "NO":
print(":)")
solves += 1
attempts += 1
current_streak += 1
if current_streak > longest_streak:
longest_streak = current_streak
elif foo == ".":
break
# For some reason this writing is not happening if I do not end menu.py
# For instance, if I were to start menu, then go into input_data and enter data, then back out into menu, and then go
# back into input_data, the data would be the same as it was at the very beginning of this example.
with open("accuracy.py", "w") as f:
f.write("attempts = {}\nDNF = {}\nsolves = {}\ncurrent_streak = {}\nlongest_streak = {}".format(attempts, DNF, solves, current_streak, longest_streak))
f.flush()
The file I am importing variables from (accuracy.py):
attempts = 251
DNF = 104
solves = 147
current_streak = 0
longest_streak = 9