Best method for a menu in a command line program?

Carl Banks pavlovevidence at
Thu Nov 4 04:02:59 CET 2010

On Nov 3, 6:19 pm, braden faulkner <brf... at> wrote:
> I'm using a menu for my command line app using this method.
> choice = "foobar"
> while choice != "q":
>     if choice == "c":
>         temp = input("Celsius temperature:")
>         print "Fahrenheit:",celsius_to_fahrenheit(temp)
>     elif choice == "f":
>         temp = input("Fahrenheit temperature:")
>         print "Celsius:",fahrenheit_to_celsius(temp)
>     elif choice != "q":
>         print_options()
>     choice = raw_input("option:")
> Just wondering if there is another or more efficient way I should be doing it?

You're not even close to the point where you have to start worrying
about efficiency.

If you're at a point where efficiency matters, then by that point you
should have long ago refactored it into a function-call dispatch
(which is what others have posted) for the sake of organizing code.

For this program, what you have it fine.

Carl Banks

More information about the Python-list mailing list