在Python中编写更紧凑的if语句

Jon*_*ris 2 python if-statement

我一直在用Python编写计算器.计算器没有任何问题,但我想让其中的部分代码更优化/缩短.

这是我想要优化/缩短的代码:

   #In this code i check to see if they have entered a valid option in my calculator
   option = int(input("Option: "))
   if option != 0:
      if option != 1:
         if option != 2:
             if option != 3:
                if option != 4:
                   print("Please enter a valid choice")
                   #As you can see it needs to check 5 numbers
Run Code Online (Sandbox Code Playgroud)

如果你能找到一种缩短上面代码的方法,那将非常感激!

Hen*_*son 5

如果您在列表中拥有所有选项,这将是最快捷,最简单的方法.

if option not in [1,2,3,4]:
   print("Not a valid choice")
Run Code Online (Sandbox Code Playgroud)