[Tutor] console text positioning

Alan Gauld alan.gauld at freenet.co.uk
Sun Nov 13 01:10:38 CET 2005


> problem** I DON'T want to see in console output
> repeated questions like:
> 
> Question one (a,b,c,d): ftbryj
> Question one (a,b,c,d): asdf
> Question one (a,b,c,d): a
> Question two (1,2,3,4): b
> Question two (1,2,3,4): 5
> Question two (1,2,3,4): 2 
> Question three (a,b,c):
> ...
> 
> I want to see only:
> 
> Question one (a,b,c,d): a
> Question two (1,2,3,4): 3
> Question three (a,b,c): c 
> Question four (1-10): 9 
> ...
> 
> So basicly, I want to see in console output only
> correct answers. 
> Any idea how to do this ??

I think the only way is to use the curses module (Unix only)
Thee is no easy way to position the cursor on the previous line 
without curses that I know of. And basically you need to go 
up a line and redraw the line to avoid multiple input lines.

No, wait a minute, thee is anothercway but its tricky to 
get right. You could use the getch() function from curses 
on Unix but also in msvcrt on Windows. That will read the 
character as the user types it without a newline. This is not 
what users normally expect however so might be more 
confusing than helpful, but even if you only used it to 
swallow the input up to a newline. As I said it needs some 
work to get right but could be made to do what you want.

In pseudo code

display prompt
inp = ''
data = ''
while data == notValid()
  while inp != '\n':
    print inp    
    inp = getch()
      data += inp

# use data here...

HTH,

Alan G
Author of the learn to program web tutor
http://www.freenetpages.co.uk/hp/alan.gauld




More information about the Tutor mailing list