0

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
barbsan
  • 3,418
  • 11
  • 21
  • 28
  • Just tested this, it is storing results after every set. It just overwrites previous results on file. Do you want to store result scores of each set? – Mohammed Jamali Aug 02 '19 at 09:31
  • see [here](https://stackoverflow.com/questions/4706499/how-do-you-append-to-a-file-in-python) on how to append to a file – FObersteiner Aug 02 '19 at 09:35

2 Answers2

0

a small mistake you have made is opening the file in write mode every time , which will clear the contents of a file and gives you the file object. Please initialize it before to make your code work. the below code change will make your code work as you expected.

  os.system('clear') # on linux / os x
  saveFile = open('results.txt', 'w')
  while equ < 5:
    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()
Rangeesh
  • 361
  • 1
  • 13
  • This is not exacly the result want I want. I want the total of good and bad awnsers. What this does is saves after ever question the result and I still need to exit before it wites to a file. I'm using the site https://repl.it/ to write the code – Daan Meerts Aug 02 '19 at 09:52
  • you want to save results after every question ? please specify how you want to save results in the file at last. – Rangeesh Aug 02 '19 at 09:55
  • Hi , made changes to above code, Does above make sense to you ? if not explain a bit like what input you give and what output you want so that we can help you out , the above will save the total no of good question and bad answers. – Rangeesh Aug 02 '19 at 10:04
  • The code what Rangeesh made saves the sesults what I want, but I still need to quit the program before it will save and not when it returns to the menu. – Daan Meerts Aug 02 '19 at 10:47
  • As soon if optellen ends and returns to the menu I want that the results are saved to the file and not after I return to the menu and need to quit. If optellend end and returns to the menu and I look in the directory the file is not made yet. If I quit then I see that the results are saved in the fiel results.txt – Daan Meerts Aug 02 '19 at 11:34
0

I think I found the problem. It writes the file, only because I use the site https://repl.it it doesn't show that it saves results.txt directly only when I quit the program. I found out because I change the code a little. I added a function so it will show what is in the direcotry. This is the code now and it works. Everyone tanks for the support. I'm sorry I didn't figured it out for my self, I'm just new the programming.

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', 'a+')
    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()

def seeresults():
  readFile = open('results.txt')
  content = readFile.read()
  readFile.close()
  print(content)
  input()

def directory():
  dir = os.listdir()
  print(dir)
  input()

while True:
  menu = ['optellen', 'directory', 'see results', '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)])
    directory()
  elif choose == str(2):
    print(menu[int(choose)])
    seeresults()
  elif choose == str(3):
    print(menu[int(choose)])
    break