[Tutor] A few newbie questions ...

alan.gauld@bt.com alan.gauld@bt.com
Mon, 21 Feb 2000 11:15:07 -0000


> 1.  Can someone point me to the module (if it exists)
> that would allow me to do such C++ type things as
> clearscreen, change the background/foreground colors,
> and especially, a python equivalent of C++'s gotoxy.

None of these are C++ things - the C++ standard library 
doesn't do any of them - they are typically PC things
(in fact DOS things). These are possible in an exclusively 
DOS world because you know what kind of hardware you will 
be using. Python is multi platform and doesn't know whether 
the terminal even has a screen, far less how to clear it.

Similarly it may not be able to show colors etc.

On unix there is a module called curses that does that 
for you within the limits of an ASCII display. 

But what happens inside a GUI window? What does 
gotoXY mean - pixels or character opositions?..

On windows there is a python module pythonwin that gives 
you access to the Win32 API, but thats quite hard to use 
for a beginner. I'd suggest moving your code to Tkinter 
- which works on unix, mac and PC.

You can see some examples on my online tutor(see Event 
Driven programming and the case study)

http://www.crosswinds.net/~agauld/

> 2.  Is it possible to have input be either a string or
> number, depending on what the user inputs?  In other
> words, I would like to create a menu with options
> #1-4, and option X and Q to quit.  Is that possible?

Yes, just use raw_input and compare character values:

from sys import exit
def func1():
    	print "func1"

MenuItem = { 	'1': func1, 
		'2': func1, 
		'3':func1, 
		'4':func1, 
		'Q':exit,
		'X':exit }

options = MenuItem.keys()
resp = raw_input("prompt")
if resp in options:
	MenuItem[resp]()
else: print "Invalid choice"

Should do it.

Alan G.