[Tutor] moving focus away from a raw_input dialog box

Kent Johnson kent_johnson at skillsoft.com
Fri Oct 22 03:55:01 CEST 2004


There is nothing wrong with structuring your program like this!

If you do it a lot you might be interested in the cmd module. It adds 
support for help and automatic command dispatch.

Here is your sample written with cmd. It's a little longer but maybe better 
structured....
from cmd import Cmd

class Hair(Cmd):
     def __init__(self):
         Cmd.__init__(self)
         self.prompt = "What is your hair color? "

     def do_green(self, line):
         print "Wow! I don't know anyone with green hair!"

     def help_green(self):
         print "green - use this command if you have green hair"

     def do_blonde(self, line):
         print "Your hair color is the same as mine."

     do_blond = do_blonde    # Allow alternate spelling of blonde

     def do_quit(self, line):
         return True # flag to exit cmdloop

     do_exit = do_quit

     def default(self, line):
         print "Your hair color is obviously not important enough to make 
it in my program."

Hair().cmdloop()


Here is a sample interaction:
D:\Personal\Tutor>python hair.py
What is your hair color? green
Wow! I don't know anyone with green hair!
What is your hair color? brown
Your hair color is obviously not important enough to make it in my program.
What is your hair color? blond
Your hair color is the same as mine.
What is your hair color? help

Documented commands (type help <topic>):
========================================
green

Undocumented commands:
======================
blond  blonde  exit  help  quit

What is your hair color? help green
green - use this command if you have green hair
What is your hair color? quit


Kent

At 07:57 PM 10/21/2004 -0500, Jacob S. wrote:
>Hello,
>
>     I'm not exactly sure of the code conditions you have, but I might be
>able to help.
>
>Instead of making 0 the exit of the whole while loop, set up a while 1 loop.
>Then you can set up if, elif, else commands to search for different commands
>received from the raw_input box.
>
>ex.
>
>     while 1:
>         ask = raw_input("What is your hair color? ")
>         if ask == "quit" or ask == "exit":
>             break  ## This breaks out of the while loop
>         elif ask == 'green':
>             print "Wow! I don't know anyone with green hair!"
>         elif ask == 'blonde' or ask == 'blond':  ## Since people spell it
>differently, check for both spellings
>             print "Your hair color is the same as mine."
>         else:
>             print "Your hair color is obviously not important enough to make
>it in my program."
>
>You can make one of the commands 'print list of commands' to print out
>possible entries for ask or something like that.
>Many of my programs are structured like that. It is probably frowned on by
>the more professional programmers, though.
>
>Hope this helps,
>Jacob Schmidt
>
>_______________________________________________
>Tutor maillist  -  Tutor at python.org
>http://mail.python.org/mailman/listinfo/tutor



More information about the Tutor mailing list