Newbie: switch question in Python

William J. King wjk at wjk.mv.com
Thu Dec 2 00:38:05 EST 1999


Hi again! I was reading a tutorial that said
there was no switch statement in Python...and
that I could use a 'conditional' if else etc.
-- so I wrote a switch and would like to know if
its ok to do this or if you have any other better
ideas... Thanks to all for their help in learning
Python in the past 2 weekends....

#Python command 'switch' based
#on a dictionary with commands that
#follow indention rules......

switch = {1: 'for m in range(1,3):\n\t\tprint m\n',
   4: 'for m in range(4,7):\n\t\tprint m\n',
   3: 'for m in range(9,11):\n\t\tprint m\n',
   6: 'print "Key", m, "was found here!"'}

for q in range(0,7):
   try:
      exec switch[q]
   except:
      print "Sorry, not in command_list"






More information about the Python-list mailing list