Best method for a menu in a command line program?

Terry Reedy tjreedy at udel.edu
Thu Nov 4 16:21:21 EDT 2010


On 11/3/2010 9:19 PM, braden faulkner 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?

Others have answered that, but I would point to the inefficiency for 
users of having to enter unit and value on two separate lines. The 
prompt could be "Enter value and unit (f, c, or k) or q to quit: "
Allow upper and lower case, with or without a space between.

-- 
Terry Jan Reedy




More information about the Python-list mailing list