[Tutor] New guy question...

David david at pythontoo.com
Tue Sep 15 01:47:45 CEST 2009


Rich Lovely wrote:
> 2009/9/14 Warren <warren at wantonhubris.com>:
>> 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.
>>
>> - Warren
>> (warren at wantonhubris.com)
>>
>>
>>
>>
>> _______________________________________________
>> Tutor maillist  -  Tutor at python.org
>> To unsubscribe or change subscription options:
>> http://mail.python.org/mailman/listinfo/tutor
>>
> 
> I'm just a little irritated at all the "Don;t use input()"-alikes...
> 
>> #!/usr/bin/env python3
> 
> The OP is in python3, so it isn't an issue.  I agree with the don't
> use python 3 emails, but that's another matter.
> 
> The way I read the OP, the problem appears to be that input() isn't
> blocking... so none of the other responses have actually replicated
> the problem described. (I can't even get the eof error by hitting
> enter on a newline) - I am on a mac, running from terminal.
> 
> I can only replicate the error one way:
> 
> $ cat test.py | python3
> Traceback (most recent call last):
>   File "<stdin>", line 1, in <module>
> EOFError: EOF when reading a line
> 
> How exactly you running your script?
> 
This works with 2.6

#!/usr/bin/python

print "Type integers, each followed by ENTER; or just ENTER to finish"

total = 0
count = 0

while True:
     line = raw_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

david [07:43 PM] opteron ~ $ ./tutor_input.py
Type integers, each followed by ENTER; or just ENTER to finish
1
2
3
4
5
6

count = 6 total = 21 mean = 3


-- 
Powered by Gentoo GNU/Linux
http://linuxcrazy.com


More information about the Tutor mailing list