[Tutor] New guy question...
Dave Angel
davea at ieee.org
Mon Sep 14 23:21:54 CEST 2009
(You're top-posting, which makes the message flow very confusing)
Warren wrote:
> <div class="moz-text-flowed" style="font-family: -moz-fixed">
> Well, I thought that as well but I took it out which makes it run
> under 2.6.1 and I get similar, but not exactly the same, output:
>
>
> Type integers, each followed by ENTER; or just ENTER to finish
>
> EOFError: EOF when reading a line
>
>
>
> - Warren
> (warren at wantonhubris.com)
>
>
>
>
> On Sep 14, 2009, at 3:57 PM, Robert Berman wrote:
>
>> Hi,
>>
>> I noticed this: #!/usr/bin/env python3.... which I think indicates
>> you are using python version 3. I strongly suspect you are reading a
>> text based on one of the version 2 issues of python.
>>
>> You might consider dropping back a version(such as 2.6.) since most
>> learning texts are not updated to work with Version 3.
>>
>> Robert Berman
>>
>>
>>
>>
>> On Mon, 2009-09-14 at 15:30 -0400, 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.
>>>
>>> - Warren
>>> (
>>> warren at wantonhubris.com
>>> )
>>>
>>>
>>>
>>>
>>> _______________________________________________
>>> Tutor maillist -
>>> Tutor at python.org
>>>
>>> To unsubscribe or change subscription options:
>>>
>>> http://mail.python.org/mailman/listinfo/tutor
>
>
> </div>
>
Seems to me you're running Python 2.x. The input() function is
dangerous. In Python 2.x you should use raw_input(), and your program
will behave as you expect.
In Python 3, raw_input() has been dropped, and that functionality is
called input().
The old input() function evaluates the input, and if there isn't
anything to evaluate, it raises an exception, as you see. But to get a
uninterpreted string, use raw_input().
DaveA
More information about the Tutor
mailing list