[Tutor] New guy question...

bob gailer bgailer at gmail.com
Mon Sep 14 22:10:54 CEST 2009


Warren wrote:
>
> Hey all,
>
> I'm just getting started with Python and I'm working my way through my 
> first "Learn Python" book on my Mac.  I ran into a weird issue 
> though.  Here's the example code I'm using:
>
> #!/usr/bin/env python3
>
> print( "Type integers, each followed by ENTER; or just ENTER to finish" )
>
> total = 0
> count = 0
>
> while True:
>     line = input()
>     
>     if line:
>         try:
>             number = int(line)
>         except ValueErr as err:
>             print( "BLARGH : ", err )
>             continue
>            
>         total += number
>         count += 1
>     else:
>         break
>        
> if count:
>     print( "count =", count, "total =", total, "mean =", total / count )
>     
>
> Now, what happens is that this starts up and immediately dies, giving 
> me this error:
>
> Type integers, each followed by ENTER; or just ENTER to finish
> Traceback (most recent call last):
>   method <module> in test.py at line 9
>     line = input()
> EOFError: EOF when reading a line
>
> Why is the "input" statement not waiting for input like it should be 
> and instead killing the app?  My google-fu is failing me on this one.

Sorry - I can't explain that. But consider a simplified program:

total = 0
count = 0
line = input()
while line:
    if line.isdigit():
        number = int(line)
        total += number
        count += 1
    else:
        print( "BLARGH : ", err )
    line = input()
if count:
    print( "count =", count, "total =", total, "mean =", total / count )

-- 
Bob Gailer
Chapel Hill NC
919-636-4239


More information about the Tutor mailing list