[Tutor] Need help with find error
Peter Otten
__peter__ at web.de
Wed Feb 4 13:32:06 CET 2015
Андрей Пугачев wrote:
> Hi
> I'm learning python a few weeks and have problems with code that read text
> from txt-file
> Code doing all right, but in the end I have error
>
> Traceback (most recent call last):
> File "trivia_challenge.py", line 81, in <module>
> main()
> File "trivia_challenge.py", line 74, in main
> category, question, answers, correct, explanation, points =
> next_block(trivia_file)
> File "trivia_challenge.py", line 37, in next_block
> points = int(next_line(the_file))
> ValueError: invalid literal for int() with base 10: ''
>
> I see it don't like empty line, but line is not emty...
>
> py-file http://pastebin.com/9C4guZq5
>
> txt-file http://pastebin.com/dZVs8V9P
Put some debugging code into your script that keeps track of the line you
are reading. For that you can modify the next_line() routine:
current_line = 0
def next_line(the_file):
"""Return next line from the trivia file, formatted."""
global current_line
line = the_file.readline()
print("XXX CURRENT LINE", current_line, repr(line))
current_line += 1
line = line.replace("/", "\n")
return line
Then, when you run the script again, there will be a line
XXX CURRENT LINE 451 'yabba dabba doo\n'
printed right before the exception is triggered. Once you know the line
(hint: it's not actually a line in the file) the problem should be easy to
fix. (Come back here to ask for more hints if you cannot fix it.)
More information about the Tutor
mailing list