例外:动作超出范围

时间:2019-08-30 08:52:26

标签: python exception

def main():
 action_type=input("enter action type")
 input_string=input("enter input string")

try:
 action_type=int(action_type)
except:
 raise ValueError("invalid action type")
if(action_type!=1 or action_type!=2):
 raise Exception("action out of range")
elif(action_type==1):
 print(input_string)

这将给出输出:Exception:action超出范围 当我输入action_type为1

1 个答案:

答案 0 :(得分:4)

在您的代码中,无论True的值如何,此条件始终 action_type

action_type != 1 or action_type != 2

特别是,当action_type1时,action_type != 2True。您真正想说的是:

if action_type != 1 and action_type != 2:

或者,更Python化:

if action_type not in (1, 2):