I’m reading in numbers from a text file. The file consists of 100 lines; each line consists of 50 digits followed by a paragraph return; no spaces, commas or quotes. The following works:
block = open(‘EP13_numbers.txt’, ‘r’)
tempsum = 0
for line in block:
try:
lineval = int(line)
tempsum += lineval
except ValueError:
break
result = str(tempsum)
print(result[0:10]) # prints the 1st ten digits of the sum of 100 50-digit numbers
This, however, does not:
tempsum = 0
with open(‘EP13_numbers.txt’, ‘r’) as block:
for line in block:
lineval = int(line)
tempsum += lineval
…
[same last two lines]
This version works for another solver of the Euler problems, but for me it generates:
“ValueError: invalid literal for int() with base 10: ‘\n’”
I’ve solved the problem which allows me to see other people’s code. I’m trying to learn Python by studying their syntax (probably a bad idea, but it’s all I can do right now). I don’t understand why the int function in the second case works for him but not for me. Doesn’t the line read in as a string in both cases?
Can anyone explain? Thanks.
PS: I’m using Py 3.7 and IDLE. Hope my indents come through.
“ValueError: invalid literal for int() with base 10: ‘\n’”
What number do you think ‘\n’ represents? It doesn’t.
The \n is an end of line marker (“newline”). When you read a blank line
from a file, you get a newline. You’re asking Python to that newline
into a number, which it obviously cannot do, so it raises an exception.
In this case, a ValueError.
An exception is the int function’s way of saying “I don’t know what to
do with this”.
Now look at the original code. It contains:
try:
... some code here, including the call to int
except ValueError:
break
In the original, when the int function raises ValueError, the
surrounding try…except block catches the exception, and breaks out of
the loop.
You could try indenting your code with four spaces on each line, or
maybe try using the web interface which I think has a text editor widget
complete with a button for formatting code. Maybe. Try it and see.
Actually, I knew that \n is a newline command. That does not mean I had a clue what the error message meant. I thought my exception was working to stop the for when it ran out of data; I didn’t realize it was taking care of the end of the line as well. (I wondered how Python was doing that! duh.) That is really great to know and ends a lot of confusion.
Still, that does not tell me how the other guy’s code worked for him… how does his syntax deal with the \n’s?
Re indents: I knew the system would left-justify my lines if I just pasted them in, so I physically put in spaces ipo tabs: no joy, they still got wiped out. I will try the web interface next time if I can figure out what that is.
If you read Steven’s first reply, he told you exactly how “the other guy’s code” dealt with the blank line. You may need to read up a little about the try... except construction. There are other ways of dealing with the problem, such as checking before you try turning the line into an integer that it isn’t a blank line.
To be clear the other guy’s code is not the try/except version, it is this one:
with open(‘EP13_numbers.txt’, ‘r’) as block:
for line in block:
lineval = int(line)
tempsum += lineval
…
For HIM, it works (allegedly); for ME, it generates the above-mentioned error which, now that I understand it better, I would expect to get here as well. So my question is, how can this code work without generating a ValueError?