Best method for a menu in a command line program?

Arnaud Delobelle arnodel at gmail.com
Thu Nov 4 03:15:33 EDT 2010


Ben Finney <ben+python at benfinney.id.au> writes:
[...]
>     commands = {
>         'q': (lambda: quit()),
>         'c': (lambda: prompt_and_convert_temperature(
>                 ["Celsius", "Fahrenheit"], celsius_to_fahrenheit)),
>         'f': (lambda: prompt_and_convert_temperature(
>                 ["Fahrenheit", "Celsius"], fahrenheit_to_celsius)),

          None: print_commands,

>         }
>
>
>     if __name__ == '__main__':
>         choice = None
>         while choice is None:
>             choice = raw_input("Command: ")

              commands.get(choice)()


-- 
Arnaud



More information about the Python-list mailing list