By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,551 Members | 1,159 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,551 IT Pros & Developers. It's quick & easy.

int(line) does not give expected results...

P: 3
And it's not me. This is taken verbatim from Mark Lutz's book "Programming Python"

you have data.txt:

123
000
999
042

You have adder.py:

import sys
sum = 0
while True:
try:
line = raw_input()
except EORError:
break
else:
sum += int(line)
print sum



Now at the command line:

user@computer:~$cat data.txt | python adder.py

This is supposed to output 1164 instead I get:

Traceback (most recent call last):
File "adder.py", line 9, in <module>
sum += int( line )
ValueError: invalid literal for int() with base 10: ''

I've looked up int() and by its description this should work. I've also tried to run it with different commands, like:

python adder.py < data.txt

python sorter.py < data.txt | python adder.py

All give the same error. I'm using version 2.5.1

(The code is indented correctly, just doesn't show up that way here.)
Mar 11 '08 #1
Share this Question
Share on Google+
4 Replies


jlm699
100+
P: 314
It's gotta be you...

works for me with one minor change...

EOFError... not EOR
Mar 11 '08 #2

P: 3
That was a typing mistake. I meant EOFError.

Are you on a linux or a windows computer?

I don't know if it would make any difference. But I can't understand how it could be me.
Mar 11 '08 #3

P: 3
Well, this is odd...

I was trying to run this script on an Ubuntu system. Gutsy 7.10

I decided to go to a WinXP system and try it with 'type' instead of 'cat'

The script worked as expected. Well, that system is a dual boot with Kubuntu Feisty Fawn. So I booted into that system and ran the scripts there. (with cat again.) This system gave me the same error as on Ubuntu.

I have one more computer that dual boots win98/zenwalk. I decided to run the script on zenwalk. The script ran correctly, just as it did on WinXp.

Now Kubuntu is pretty much the same thing as Ubuntu. But Zenwalk is Slackware. So, I'm wondering if it has something to do with the system?
Mar 11 '08 #4

bgeddy
P: 16
Were you using the same numbers file across your tests ? Looks like the numbers file that is failing has a blank or empty line after the last number. Try removing it with a line editor like Vim/ed/pico to make sure no extra line feeds or empty lines exist..

Just as a tip - try this slight addition to the script :
Expand|Select|Wrap|Line Numbers
  1. import sys
  2. sum = 0
  3. while True:
  4.     try:
  5.         line = raw_input()
  6.     except EOFError:
  7.         break
  8.     else:
  9.         print "Hey - I've got a line line:'%s'" % line
  10.         sum += int(line)
  11.         print sum
It some times helps when you have a bug like this to put in a print statement to see what python is getting !
Mar 12 '08 #5

Post your reply

Sign in to post your reply or Sign up for a free account.