[Tutor] Built-in function to check a character is ASCII character?

Luke Paireepinart rabidpoobear at gmail.com
Sat Mar 17 03:37:50 CET 2007


ammar azif wrote:
> Is there any Built-in function to check whether  a character is an 
> ASCII character?
>
> I am trying to learn event driven programming in python. Heres my code:
>
> while 1:
>     key=msvcrt.getch()
>     if key==255:
>         continue
Here's your problem.
 >>> import msvcrt
 >>> msvcrt.getch()
'\xff'
 >>> print '\xff'
ÿ
 >>> '\xff' == 255
False
 >>> ord('\xff')
255
 >>> '\xff' == chr(255)
True

Do you see?
'\xff' is a character, so it's not equal to the integer value 255 (in 
Python, characters are not the same thing as integers, unlike C++)
If you get the ordinal of '\xff' you'll get 255, or if you do chr(255) 
you'll get '\xff'.
You need to use one or the other method in your comparison, or just 
compare to '\xff'.



More information about the Tutor mailing list