473,395 Members | 1,653 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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

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
4 1431
jlm699
314 100+
It's gotta be you...

works for me with one minor change...

EOFError... not EOR
Mar 11 '08 #2
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
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
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

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

Similar topics

3
by: Ton K. | last post by:
Folks, I get subject when binding <Button-1> to a listbox. The value for h is '??' which triggers the error. I'm running Python 2.2.2 from SuSE. Please help TK
0
by: python | last post by:
Hi- I'm getting a "DeprecationWarning: integer argument expected, got float" warning with mx.DateTime and I can't figure out why. Can anyone help me out? This code: import mx.DateTime
3
by: Tariq | last post by:
This might sound like a stupid question, but anywas when you assign an int to a textbox via the textbox.text propperty I assume boxing happens. If this assigment happend too often rapidly on a...
4
by: John Bailo | last post by:
In Visual Studio.2003 I set up two command line arguments in the Project Properties. The arguments are XFER 63 Written just like that. If I example the value of args it shows its equal...
40
by: Spiros Bousbouras | last post by:
Do you have an example of an implementation where sizeof(short int) does not divide sizeof(int) or sizeof(int) does not divide sizeof(long int) or sizeof(long int) does not divide sizeof(long long...
3
by: flickimp | last post by:
Hi Please help with this: When I enter Select 2800 / 1000 I get 2, but I am expecting 2.8
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.