-3

trying to make this program continuously loop until they input a valid selection from the first menu, options 1-6. So far I it only will only ask 1 more time and then if they input an invalid character it goes straight to terminate message. For a class I'm taking and very new to this program. Any help would be appreciated. I did notice that if I post the

if Choice not in [1,2,3,4,5,6]: print ("\n",Choice, "is not a valid choice.\n") print ("Please select choose from the available options.\n") menu() Choice = int(input("What type of Conversion would you like to execute? Enter Choice(1-6): "))

section after elif Choice==6 it will loop another time but that's it.

#   Imperial - Metric Conversion Calculator
#   This Program Calculates the conversions from Imperial to Metric units
#   or Metric to Imperial units. Users first select the type of conversion
#   they wish to execute, then they enter their value and the program converts
#   it. They will then be asked if they wish to execute another conversion,
#   if yes the process would be repeated, if no the program will terminate.
#
#   N   = Number of Units, for the calculation
#   R   = Result from calculation
#   Initialization
terminate = False
while not terminate:
    
#   Welcome Message
    print("\n""Created By Name \n")
#   Instructions
    print("Welcome to the Imperial/Metric Conversion Calculator,")
    print("please select the type of Conversion you would like to")
    print("perform from the list.\n")
#   Define Menu
    def menu():
        print("1.Ounces to Grams Conversion.\n")
        print("2.Grams to Ounces Conversion.\n")
        print("3.Inches to Centimeters Conversion.\n")
        print("4.Centimeters to Inches Conversion.\n")
        print("5.Miles to Kilometers Conversion.\n")
        print("6.Kilometers to Miles Conversion.\n\n")
    menu()
    
#   User Choice Input
    Choice = int(input("What type of Conversion would you like to execute? Enter Choice(1-6): "))
#   Calculations, if/elif Statements, Output
    if Choice not in [1,2,3,4,5,6]:
        print ("\n",Choice, "is not a valid choice.\n")
        print ("Please select choose from the available options.\n")
        menu()
        Choice = int(input("What type of Conversion would you like to execute? Enter Choice(1-6): "))
        
#   Process User Input, Display Result, Ounces to Grams
    if Choice==1:
        N = float(input("\n""Please enter the Number of Ounces: "))
        R = N * (28.3495231)
        print('\n \n'"Based on the units you entered the conversion from Ounces to Grams is {:,.2f}".format(R))
#   Process User Input, Display Result, Grams to Ounces        
    elif Choice==2:
        N = float(input("\n""Please enter the number of Grams?: "))
        R = N / (28.3495231)
        print('\n \n'"Based on the units you entered the conversion from Grams to Ounces is {:,.2f}".format(R))
#   Process User Input, Display Result, Inches to Centimeters
    elif Choice==3:
        N = float(input("\n""Please enter the number of Inches?: "))
        R = N * (2.54)
        print('\n \n'"Based on the units you entered the conversion from Inches to Centimeters is {:,.2f}".format(R))
#   Process User Input, Display Result, Centimeters to Inches
    elif Choice==4:
        N = float(input("\n""Please enter the number of Centimeters?: "))
        R = N / (2.54)
        print('\n \n'"Based on the units you entered the conversion from Centimeters to Inches is {:,.2f}".format(R))
        
#   Process User Input, Display Result, Miles to Kilometers
    elif Choice==5:
        N = float(input("\n""Please enter the number of Miles?: "))
        R = N * (1.609344)
        print('\n \n'"Based on the units you entered the conversion from Miles to Kilometers is {:,.2f}".format(R))
#   Process User Input, Display Result, Kilometers to Miles        
    elif Choice==6:
        N = float(input("\n""Please enter the number of Kilometers?: "))
        R = N / (1.609344)
        print('\n \n'"Based on the units you entered the conversion from Kilometers to Miles is {:,.2f}".format(R))
        
#   Thank You Message
    print('\n\n'"Thank you for using the Imperial - Metric Conversion Calculator! \n")
#   Terminate Check
    response =input("Would you like to perform another conversion? (y/n): ")
    while response != 'y' and response != 'n':
        response = input("Please enter 'y' or 'n' : ")
        
    if response == "n" :
        terminate = True
  • 2
    Have you done any debugging? I would recommend reading https://ericlippert.com/2014/03/05/how-to-debug-small-programs/. – AMC Oct 15 '20 at 21:15
  • 3
    Does this answer your question? [Asking the user for input until they give a valid response](https://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response) – ChrisGPT was on strike Oct 15 '20 at 21:16

1 Answers1

0

You can try this:

while True:
    try: 
        choice = int(input("What type of Conversion would you like to execute? Enter Choice(1-6): ")))
        if choice not in [1, 2, 3, 4, 5, 6]:
            raise ValueError
        break
    except ValueError:
        print ("\n",Choice, "is not a valid choice.\n")
        print ("Please select choose from the available options.\n")
        menu()

Does this help?

Rolv Apneseth
  • 2,078
  • 2
  • 7
  • 19
  • This is to replace the `Choice = int(input...` section as well since if somebody enters a letter or word it would raise a value error there too so it needs to be caught – Rolv Apneseth Oct 15 '20 at 21:45