0

I am creating a quiz in which each users score is saved to an external text file. However, whenever I output a report for the highest score in the maths easy quiz it says: ValueError: invalid literal for int() with base 10: ''

This seems to be the line with the problem: if highestScore <= int(line.strip()):

        with open("mathsEasy.txt") as mathsEasyFile:
        highestScore = 0
        for line in mathsEasyFile:
            if highestScore <= int(line.strip()):
                highestScore = int(line.strip())
    mathsEasyFile.close()

    print "The highest score is", highestScore

Basically, everytime a user does the maths easy quiz it saves their score to the text file called mathsEasy.txt The text file looks like this: the username : score for example Kat15 : 4 I need to output the highest score only, not the username as well.

  • Is your indenting off? Nevertheless, this error is saying that `line.strip()` is the empty string (`''`), so when it tries to parse it as an integer, Python throws an error. You likely have empty lines in your file – Niema Moshiri Jan 29 '18 at 01:47
  • No I checked my file and it has information it it –  Jan 29 '18 at 01:51
  • I'm not implying that the entire file is empty, but if any of the lines are empty, your code will throw that error. I would suggest checking if the line is empty before attempting to parse it as an integer, e.g. `if len(line.strip()) != 0:`. Or, you can simply add it to that existing if-statement, e.g. `if len(line.strip()) != 0 and highestScore <= int(line.strip()):` – Niema Moshiri Jan 29 '18 at 02:06
  • Okay. What does parse mean? –  Jan 29 '18 at 02:08

2 Answers2

1

Now that you've added the example of how the file works:

with open("mathsEasy.txt") as mathsEasyFile:
    highestScore = max(int(line.split(' : ')[1]) for line in mathsEasyFile if len(line.strip() != 0)
print("The highest score is %d" % highestScore)

Broken down to help you better understand it:

highestScore = 0 # in my previous code, I use max() instead
with open("mathsEasy.txt") as mathsEasyFile: # open the file
    for line in mathsEasyFile: # for each line in the file,
        if len(line.strip()) == 0: # if the current line is empty,
            continue # ignore it and keep going to the next line
        _,score = line.split(' : ') # split the line into its components
        if int(score) > highestScore: # if this score is better,
            highestScore = int(score) # replace the best score
print("The highest score is %d" % highestScore)
Dhia
  • 10,119
  • 11
  • 58
  • 69
Niema Moshiri
  • 909
  • 5
  • 14
0

A few things...

  • it could just be formatting in StackOverflow, but ensure your indentation is correct; after mathsEasyFile: you probably want everything but that final print statement indented.
  • I think you're going to want to add something like lines = mathsEasyFile.readlines() and iterate through that with something like for line in lines:. Your current setup isn't reading any of the file contents, I don't believe. Incorrect. See comment below.
  • mathsEasyFile.close() is already done for you by using the with statement

Clarification Edit

# something like this ought to work
with open("mathsEasy.txt") as mathsEasyFile:
    highestScore = 0
    for line in mathsEasyFile:
        if highestScore <= int(line.strip()):
            highestScore = int(line.strip())

print "The highest score is", highestScore
burling
  • 409
  • 2
  • 5
  • "I think you're going to want to add something like lines = matsEasyFile.readlines() and iterate through that like for line in lines:. Your current setup isn't reading any of the file contents, I don't believe." This is incorrect. `for line in filestream:` is valid Python syntax. The issue is an empty line – Niema Moshiri Jan 29 '18 at 01:48
  • I stand corrected regarding the filestream. Thanks for the correction. – burling Jan 29 '18 at 01:51
  • The error in the original code was from trying to parse an empty line with `int()`, and this code will still throw the same error. You should add a check to ensure the line is not empty, e.g. `if len(line.strip()) != 0 and highestScore <= int(line.strip()):` – Niema Moshiri Jan 29 '18 at 02:08
  • That's reasonable – burling Jan 29 '18 at 02:17