[Tutor] Help concerning raw_input urgently needed!

Gregor Lingl glingl@aon.at
Mon, 02 Sep 2002 08:54:32 +0200


Kyle Babich schrieb:

>On Mon, 02 Sep 2002 00:01:29 +0200, "Gregor Lingl" <glingl@aon.at>
>said:
>  
>
>>...
>>Some month ago I copied sitecustomize.py with
>>appropriate locale-setting to C:\Python22\Lib
>>thanks to an advice of someone on this list.
>>
>>This worked well until today I made the observation,
>>that raw_input doesn't work correctly:
>>
>> >>> a = raw_input("Hä? ")
>>Hä? Tschüs
>>Traceback (most recent call last):
>>  File "<pyshell#2>", line 1, in ?
>>    a = raw_input("Hä? ")
>>TypeError: object.readline() returned non-string
>> >>>
>>
>>    
>>
>
>The program expects the raw_input to return a string, but Tschüs
>according to python doesn't qualify.
>
>  
>
>>>>type("Tschüs")
>>>>        
>>>>
>UnicodeError: ASCII encoding error: ordinal not in range(128)
>  
>
That's what Python does, if not "sitecustomized". If I put the lines

import sys, locale
loc = locale.getdefaultlocale()
if loc[1]:
    encoding = loc[1]
if encoding != "ascii":
    sys.setdefaultencoding(encoding)

into the file sitecustomize.py this is repaired and  type("Tschüs") as 
well as ather output
with öäßÜ and so on works well.

Nevertheless there remains the fact that - as shown above - 
raw_input("Hä? ")
does write Hä!, does wait for input, but does NOT accept Tschüs as input.
Moreover this occurs only within IDLE, not when using the 
python-interactive-interpreter.

>A better idea might be to use sys.stdin.readline().
>  
>
>>>>import sys
>>>>a = sys.stdin.readline()
>>>>        
>>>>
>Tschüs
>  
>
>>>>print a
>>>>        
>>>>
>Tschüs
>
>  
>
This works  also on my machine - so it's a possible solution.
Nevertheless I'd prefer much if the
IDLE-bug could be repaired, e.g. by some other sort of customizing,
because in the lessons for beginning programmers, where I use it, the
introduction and explanation of sys.stdin.readline() would distract from the
main themes covered there.
Moreover it doesn't look fine, if - on a very introductory level - you 
have to
learn how to work around bugs.

Nevertheless: thanks so far, it constitutes some progress -
better this way than no way.
With kind regards
Gregor

P.S.: To have to use something successfully or
to have to explain something to others successfully
makes a *big* difference.