2

I have created an arithmetic quiz which lets the user input their name, and then 10 questions are generated. Their score is saved in a text file. I now have to save only the last three scores that they have achieved.

This is coursework so please don't give me full answers, just possibly some guidance and explanation.

I'm trying to get the names of the users into a list without the scores, so that I will be able to process them in some way. I'm using this code to create a list:

class1_list=[]
file=open("Class1.txt")
for line in file:
    line.replace("\n", "")
    line.split("scored")
    file.close()
    print(class1_list)

But I keep getting an error and I'm unsure why:

Traceback (most recent call last):
  File "D:\Python\Task 3.py", line 56, in <module>
    for line in file:
ValueError: I/O operation on closed file.

The layout of the scores in my text file looks like this:

John scored 7
Hannah scored 5
Tom scored 10
Jim scored 3

etc.

I'm a complete beginner with coding so would you please be able to explain in idiot terms what this error means?

Thanks for any replies.

Megayeni
  • 29
  • 3
  • 5
    You may want to get familiar with [How do I ask and answer homework questions?](http://meta.stackexchange.com/questions/10811/how-do-i-ask-and-answer-homework-questions), as well as [How do I ask a good question?](http://stackoverflow.com/help/how-to-ask) and [How to create a Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve). – Łukasz Rogalski Mar 22 '16 at 18:21
  • Thanks is this any better or not? – Megayeni Mar 22 '16 at 18:43
  • your question, if salvaged, may be reopened by community. Hint about your issue: according to your code, what string should be taken as a source to your 'replace' operation? Is it a `line`? Where it's said that is should be a `line`? – Łukasz Rogalski Mar 22 '16 at 18:48
  • `line=file.read()` <--- This is not a line, it is the entire contents of the file. If you want to loop over the lines of a file, just do `for line in file:` – OneCricketeer Mar 22 '16 at 19:03

0 Answers0