[Tutor] menu based programs
Alan Gauld
alan.gauld at btinternet.com
Sat Jan 17 03:00:28 CET 2015
On 16/01/15 23:39, Siya 360 wrote:
> i want to learn how to write code to interact with the user,
> in a sequence where
> if option 1, then display a,
> if 2 then display b,
> if 3 display c,
> if 4 display exit,
> if 1 selected, leads to option a, which in turn has it own options
You don't say how these menus are presented.
One option is the cmd module which has a lot of built in features such
as context sensitive help screens. But it is similar in style ton the
Python help() command which may not be what you want.
Simple text based menus are just built using print statements
(triple quoted multi-line strings often help a lot)
Here is a very simple example modelled on your description
#####################
def readMenu(menu):
while not (1<= choice <= len(menu) ):
for index, item in enumerate(menu,start=1):
print (index, item)
choice = input("\nchoose an item: ")
return choice-1
def display(choice):
print( 'ABC'[choice] )
menu = ['display A','display B', display C', 'exit']
while True:
choice = readMenu(menu)
if choice == 3:
break
else: display(choice)
########################
You can use a table driven approach with a data structure to link menu
choices to screens. But without knowing what you want the application to
look like it's hard to give solid advise
And if you want a GUI then it's a whole new ball game...
hth
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos
More information about the Tutor
mailing list