[Tutor] Numbers & Characters As Dictionary Keys

Don Parris webdev at matheteuo.org
Mon Jun 20 20:10:54 CEST 2005

On Mon, 20 Jun 2005 09:11:53 +0100
"Alan G" <alan.gauld at freenet.co.uk> wrote:

> > "raw_input", but I get "H is not defined" when I run the script.  
> > Essentially, I'd like the user to enter a number for most items, 
> > but use letters for "Help", "Quit", and "Back to Main".
> Are you sure? That kind of inconsistent input is one of the 
> big no-nos of user interface design. It usually confuses 
> the heck out of users!

Hmmm, good point.  It was a thought.  I am trying to avoid confusing my
users.  If this kind of input will cause confusion, I should avoid it.  I
was looking to provide a consistent help/quit/main single-key scheme across
all menus. I can (and presently am) using a double-digit number for this
purpose in the current script.  On the other hand, I thought people would
easily recognize at least the "H" & "Q" for "help" and "quit". 
Alternatively, I could link these to "F1" & [Ctrl]+Q, or something like
that.  Many people would understand "F1".

Basically, the sub-menu offers choices:
1-3 - add/edit/delete
4-? - standard reports
10-12 - H/Q/M 

> Hoewever to the problem at hand.
> It should just be a case of changing the keys in the dictionary.
> Unfortunately you've told us the problem but shown us the code 
> that woreks, not the broken copde. So we can only guess what 
> you might have done!
> But basically here is a sample program that does approximately 
> what you want:
> def hello(): print 'hello'
> def goodbye(): print 'goodbye'
> menu = { '1' : ('hello', hello),
>          'Q' : ('goodbye', goodbye)}
> for m in menu.keys():
>    print "%s\t%s" % (m,menu[m][0])
I am curious what the "%" by itself is doing.

> cmd = raw_input('pick one ').upper()
> menu[cmd][1]()
> Does that help?
I'll tinker with this a bit.

evangelinux    GNU Evangelist
http://matheteuo.org/                   http://chaddb.sourceforge.net/
"Free software is like God's love - you can share it with anyone anytime

More information about the Tutor mailing list