I'm trying to save results to a file after finishing a sequense of questions. The problem if the questions are done it returns to the menu only doesn't save the results to the file. If I quit the program, it will save the results. How can I make it so that is saves after the questions?
I search the internet for while loops and saving. I'm already searching for 2 days and trying to change different code with no result. I'm trying to learn programming and I'm very new to it.
import os
import random
def optellen():
equ = 0
good = 0
false = 0
os.system('clear') # on linux / os x
while equ < 5:
saveFile = open('results.txt', 'w')
for i in range(1):
eggs = (random.randint(1, 20))
bacon = (random.randint(1, 20))
ham = (eggs+bacon)
print(eggs, '+', bacon, '=', end=' ',)
test = input()
if test == str(ham):
good += 1
print(test, 'is the good awnser')
else:
print(test, ' is the wrong awnser it should be ', ham)
false += 1
equ += 1
print('You have', good, 'awnsers good and', false, 'false')
result = (good, false)
saveFile.write(str(result))
saveFile.close()
while True:
menu = ['optellen', 'aftrekken', 'vermenigvuldigen', 'quit']
os.system('clear') # on linux / os x
for i in range(len(menu)):
print(str(i) + ' ' + menu[i])
choose=input()
if choose == str(0):
print(menu[int(choose)])
optellen()
elif choose == str(1):
print(menu[int(choose)])
aftrekken()
elif choose == str(2):
print(menu[int(choose)])
vermenigvuldigen()
elif choose == str(3):
print(menu[int(choose)])
break