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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • bustBuddy
    New Member
    • Mar 2008
    • 3

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

    And it's not me. This is taken verbatim from Mark Lutz's book "Programmin g 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.)
  • jlm699
    Contributor
    • Jul 2007
    • 314

    #2
    It's gotta be you...

    works for me with one minor change...

    EOFError... not EOR

    Comment

    • bustBuddy
      New Member
      • Mar 2008
      • 3

      #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.

      Comment

      • bustBuddy
        New Member
        • Mar 2008
        • 3

        #4
        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?

        Comment

        • bgeddy
          New Member
          • Aug 2007
          • 16

          #5
          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 :
          Code:
          import sys
          sum = 0
          while True:
          	try:
          		line = raw_input()
          	except EOFError:
          		break
          	else:
          		print "Hey - I've got a line line:'%s'" % line
          		sum += int(line)
          		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 !

          Comment

          Working...