python - 新手引发异常
问题描述
这是我第一次尝试编写除发那科 CNC 控制器以外的任何东西。我不知道为什么,但这段代码返回两个异常,如果我从 else 中删除 raise Exception:它不会返回任何一个异常。我错过了什么?
cut_type=[input("Enter cut type Straight(S) or Arc(A)" )]# Add Taper later
try:
if 'S' in cut_type:
valid_type=['Y']
elif 's' in cut_type:
valid_type=['Y']
elif 'A' in cut_type:
valid_type=['Y']
elif 'a' in cut_type:
valid_type=['Y']
else:
valid_type=['N']
raise Exception('Cut Type not Valid')
except:
print('NOT VALID TYPE')
raise Exception('Cut Type not Valid')
print(valid_type)
如果我运行上面的代码,我会得到以下
Enter cut type Straight(S) or Arc(A)q
NOT VALID TYPE
Traceback (most recent call last):
File "Chip Break testing.py", line 21, in <module>
raise Exception('Cut Type not Valid')
Exception: Cut Type not Valid
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "Chip Break testing.py", line 24, in <module>
raise Exception('Cut Type not Valid')
Exception: Cut Type not Valid
如果我在从任何一个位置删除 raise Exception 的情况下运行它,我不会得到任何异常。我知道我错过了一些简单的东西,但我看不到它。
解决方案
这是我在提出建议并重新查看我的代码以使其正常工作后所做的。
cut_type=[input("Enter cut type Straight(S) or Arc(A)" )]# Add Taper later
if 'S' in cut_type:
pass
elif 's' in cut_type:
pass
elif 'A' in cut_type:
pass
elif 'a' in cut_type:
pass
else:
raise Exception('Cut Type not Valid')
谢谢大家。我希望能学到更多知识并转行。
推荐阅读
- ibm-doors - 无法使用 dxl 保存导出的 excel
- flutter - Flutter - 使用 ListView.builder 索引和提供程序包从列表中删除项目
- javascript - JavaScript html 中未定义 firebase
- python - 输入数字,输出字母表中的第n个字母
- javascript - forEach 不改变数组的值
- ios - 查看为符合类型生成的协议见证表的方法?
- javascript - 正则表达式删除降价链接中的空格
- c# - 使用 CDK 部署带有容器映像的 .NET 5 AWS Lambda:内部服务器错误
- java - java.lang.NoClassDefFoundError:从 WSDL 生成 Java 类时出现 javax/xml/soap/SOAPException
- javascript - TypeError:模式路径`genre.type`的值无效,得到值“未定义”。请帮我