Python 您好,正在尝试使该程序不断循环,直到他们从第一个菜单输入有效的选择

Python 您好,正在尝试使该程序不断循环,直到他们从第一个菜单输入有效的选择,python,Python,尝试使该程序持续循环,直到他们从第一个菜单选项1-6中输入有效选项。到目前为止,我只会再问1次,然后如果他们输入一个无效字符,它会直接终止消息。对于我正在上的一门课来说,这是一门非常新的课程。 任何帮助都将不胜感激。我确实注意到如果我发布 如果选择不在[1,2,3,4,5,6]中: 打印(“\n”,选项“不是有效选项。\n”) 打印(“请从可用选项中选择。\n”) 菜单() Choice=int(输入(“您希望执行什么类型的转换?输入Choice(1-6):”) elif Choice==6之后

尝试使该程序持续循环,直到他们从第一个菜单选项1-6中输入有效选项。到目前为止,我只会再问1次,然后如果他们输入一个无效字符,它会直接终止消息。对于我正在上的一门课来说,这是一门非常新的课程。 任何帮助都将不胜感激。我确实注意到如果我发布

如果选择不在[1,2,3,4,5,6]中: 打印(“\n”,选项“不是有效选项。\n”) 打印(“请从可用选项中选择。\n”) 菜单() Choice=int(输入(“您希望执行什么类型的转换?输入Choice(1-6):”)

elif Choice==6之后的部分将在另一个时间循环,但仅此而已

#   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
您可以尝试以下方法:

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()

这有帮助吗?

您进行过任何调试吗?我建议你阅读。这能回答你的问题吗?这将取代
Choice=int(input…
部分,因为如果有人输入字母或单词,也会在那里引发值错误,因此需要捕获它