【发布时间】:2017-03-14 00:44:15
【问题描述】:
我试图在 python3.5 中手动处理除以零异常,但是,它一直忽略我的用户定义的情况并给出默认异常。我怎样才能避免这种情况?任何帮助表示赞赏。
注意:我使用列表作为堆栈,因此是最后一个,并且正在检查顶部元素是否为 0。此外,顺序是正确的 - 我故意将顶部元素作为分母
elif 'div' in command:
if len(stack)!=0 and len(stack)!=1 and is_digit(stack[len(stack)-2]) and is_digit(stack[len(stack)-1]) and stack[len(stack)-1]!=0:
op2 = stack.pop();
op1 = stack.pop();
stack.append(str( int(int(op1) / int(op2)) ) + '\n');
else:
stack.append(':error:\n');
这给了 ZeroDivisionError: 除以零 INSTEAD of :error:
【问题讨论】:
标签: python python-3.x integer-division divide-by-zero