为什么 except 在捕获错误后不允许我输入正确的值?
Why does except not allow me to enter correct value after catching error?
我有一个变量 'action',我想从用户那里输入并验证它,我有一个 while 循环和一个 try-except 块来捕获引发错误或无效的值其余代码的功能
while True: # Input and validation of 'action'
try:
action = int(input('Please enter what you would like to do: '))
if action < 0:
negativeError = ValueError('This value is negative')
raise negativeError
if action != 1 or action != 6:
invalidValueError = ValueError('Not a valid option')
raise invalidValueError
break
except:
ValueError or TypeError
print('That is invalid, please enter a valid number corresponding to one of the actions above')
continue
如果我在第一次迭代中输入例如 1,它会按预期跳出循环,但如果我在第一次迭代中输入 -5(捕获除外)然后在第二次迭代中输入 1,它会捕获它好像 1 是一个无效值并且不会跳出循环并继续迭代。我是错误处理的新手,因此将不胜感激。
我认为它应该是这样的:
while True: # Input and validation of 'action'
try:
action = int(input('Please enter what you would like to do: '))
if action < 0:
negativeError = ValueError('This value is negative')
raise negativeError
if action not in [1, 6]:
invalidValueError = ValueError('Not a valid option')
raise invalidValueError
break
except ValueError or TypeError:
print('That is invalid, please enter a valid number corresponding to one of the actions above')
continue
action != 1 或 action != 6 始终为真。
- 您的 if 语句不正确。
- 打印 except 引发期间生成的消息,而不是创建新消息。
while True: # Input and validation of 'action'
try:
action = int(input('Please enter what you would like to do: '))
if action < 0:
negativeError = ValueError('This value is negative')
raise negativeError
if not (action == 1 or action == 6):
invalidValueError = ValueError('Not a valid option')
raise invalidValueError
break
except (ValueError,TypeError ) as e:
print(e)
尝试这样的事情。我们现在将使用默认参数值。这可以稍后修改(即由调用者传递)从而避免修改此代码的需要:
def get_action(valid_actions=[1,6]):
action_list = ','.join(map(str, valid_actions))
while True:
try:
input_ = input(f'Please select an action from this list {action_list}: ')
response = int(input_)
if response in valid_actions:
return response
raise ValueError
except ValueError:
print('Invalid response. Try again')
get_action()
随后您可能想要添加另一个操作 - 例如 3。在这种情况下:
get_action([1,3,6])
...这就是您所需要的
我有一个变量 'action',我想从用户那里输入并验证它,我有一个 while 循环和一个 try-except 块来捕获引发错误或无效的值其余代码的功能
while True: # Input and validation of 'action'
try:
action = int(input('Please enter what you would like to do: '))
if action < 0:
negativeError = ValueError('This value is negative')
raise negativeError
if action != 1 or action != 6:
invalidValueError = ValueError('Not a valid option')
raise invalidValueError
break
except:
ValueError or TypeError
print('That is invalid, please enter a valid number corresponding to one of the actions above')
continue
如果我在第一次迭代中输入例如 1,它会按预期跳出循环,但如果我在第一次迭代中输入 -5(捕获除外)然后在第二次迭代中输入 1,它会捕获它好像 1 是一个无效值并且不会跳出循环并继续迭代。我是错误处理的新手,因此将不胜感激。
我认为它应该是这样的:
while True: # Input and validation of 'action'
try:
action = int(input('Please enter what you would like to do: '))
if action < 0:
negativeError = ValueError('This value is negative')
raise negativeError
if action not in [1, 6]:
invalidValueError = ValueError('Not a valid option')
raise invalidValueError
break
except ValueError or TypeError:
print('That is invalid, please enter a valid number corresponding to one of the actions above')
continue
action != 1 或 action != 6 始终为真。
- 您的 if 语句不正确。
- 打印 except 引发期间生成的消息,而不是创建新消息。
while True: # Input and validation of 'action'
try:
action = int(input('Please enter what you would like to do: '))
if action < 0:
negativeError = ValueError('This value is negative')
raise negativeError
if not (action == 1 or action == 6):
invalidValueError = ValueError('Not a valid option')
raise invalidValueError
break
except (ValueError,TypeError ) as e:
print(e)
尝试这样的事情。我们现在将使用默认参数值。这可以稍后修改(即由调用者传递)从而避免修改此代码的需要:
def get_action(valid_actions=[1,6]):
action_list = ','.join(map(str, valid_actions))
while True:
try:
input_ = input(f'Please select an action from this list {action_list}: ')
response = int(input_)
if response in valid_actions:
return response
raise ValueError
except ValueError:
print('Invalid response. Try again')
get_action()
随后您可能想要添加另一个操作 - 例如 3。在这种情况下:
get_action([1,3,6])
...这就是您所需要的