[Tutor] Error Message

alan.gauld@bt.com alan.gauld@bt.com
Mon, 29 Jan 2001 15:22:47 -0000


> can't quite seem to work around. This is my code:

Hmmm, I think I recognise this :-)

> 
> import msvcrt, sys
> 
> print"""
> Type a key to see its ASCII value.
> Hit the space bar to end.
> """
> 
> while 1: #1 is true, so loop forever
>     key = msvcrt.getch()
>     if key != ' ':
>         # handle special keys; real code is second
>         if (key == '\000') or (key ==  '\xe0'):
>             key = msvcrt.getch()
>         doKeyEvent(key)
>     else:
>         doQuitEvent(key)
> 
> 

The code above calls doKeyEvent but you haven't 
defined it yet!

> def doKeyEvent(key):
>     print ord(key)
> 
> def doQuitEvent(key):
>     sys.exit()

Move these two functions to the top of the file and 
it should work. I'll reword the tutorial page to make 
that clearer.

lso note that in IDLE you get an infinite loop that 
you can't break out of (at least I do, and don't know 
why... I assume because IDLE is running within Tks loop...) 
So you need to run the program from a DOS prompt.

Sorry about that,

Alan G.


> 
> After I save this and run it, Python replies with this:
> 
> Type a key to see its ASCII value.
> Hit the space bar to end.
> 
> Traceback (innermost last):
>   File "C:/Program Files/Python20/Code/eventloop.py", line 14, in ?
>     doKeyEvent(key)
> NameError: There is no variable named 'doKeyEvent'
> 
> I'm stuck since doKeyEvent is a function, not a variable. Why 
> doesn't Python 
> recognize it as such?
> 
> Thanks,
> 
> Britt
> _________________________________________________________________
> Get your FREE download of MSN Explorer at http://explorer.msn.com
> 
> 
>