[Tutor] menu based programs

Alan Gauld alan.gauld at btinternet.com
Sat Jan 17 10:06:39 CET 2015


On 17/01/15 02:00, Alan Gauld wrote:

> 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

Oops, a couple of bugs in there, here is a working version

def readMenu(menu):
     choice = 0
     while not (1<= choice <= len(menu) ):
        for index, item in enumerate(menu,start=1):
            print (index, item)
        choice = int(input("\nchoose an item: "))
     return choice-1


-- 
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