Hi,

I was playing with the code below, and I was wondering if there was a way to 
populate the dictionary called 'commands' (in the function 'check_command()').

Suppose I would add another function, which I would also like to store as a 
value in 'commands', could it simply be programmed, or would every 
update/addition require the definition of the dictionary to be extended?
It would be most desirable if one could simply add another function without 
there being the need to touch any of the other code. 

Cheers!!
Albert-Jan()

def foo (a):
    return "foo" * a

def bletch (q):
    for i in range(20):
        print i * q

def stilton (n):
    print "yes sir, " * n

def romans (z):
    print "what have the romans really done for us?\n" * z

def check_command():
    commands = {'1': foo, '2': bletch, '3': stilton, '4': romans}
    while True:
        select = raw_input("choose an option [%s]: " % 
"|".join(sorted(commands.keys())))
        if select in commands.keys():
            check_parameter(commands, select)
            break
        else:
            print "illegal option (%s)" % select

def check_parameter(commands, select):
    while True:
        parameter = raw_input("choose a parameter [integer]: ")
        if parameter.isdigit():
            commands[select](int(parameter))
            break
        else:
            print "illegal parameter (%s)" % parameter

check_command()



      
_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

Reply via email to