您可以使用if-elif-else语句解决此问题。为了使它看起来像,它将要求一个有效的选项,直到给定的选项在列表中为止,我们可以使用while循环。当该选项有效时,请中断循环,否则将反复请求输入。
您应该将输入作为整数,因为这需要使用int()
方法将输入类型转换为整数。
请检查代码以遵循给定的要点。
print("Come-on in. Need help with any bags?") while True: # loop is used to take option until it is not valid. bag = int(input("(1)Yes (2)No Thanks (3)I'll get 'em later\nTYPE THE NUMBER OF YOUR RESPONSE: ")) if bag == 1: print("You have chosen YES. We'll help with bags") break # Stop the loop as the option is valid elif bag == 2: print("Ok you don't want any help.") break elif bag == 3: print("Tell us, when you want the help") break else: print("Invalid Choice, Please select number from 1 to 3")