有没有办法在除块之外打印自然错误消息?
我正在练习错误处理,我有一个问题。您可以做到吗
try:
a = int(input('Please enter the first number it must be >= 200' + '\n' + '(note the sum of both numbers must be <=300)'))
b= int(input('please your second number it must be <= 50'))
c = a + b
assert a >=200 and isinstance(a, int), 'invalid entry for a must be <= 200 and a number'
assert b<=50 and isinstance(b, int), 'invalid entry for b must be <=50 and a number'
assert c <= 300, 'The sum of your numbers is >300'
except ValueError
print('error')
else:
print('All in range')
I am practicing with error handling and I have a question. can you make it so the error messages in the assert will be displayed in the except block right now if a error occurs only 'error' is printed and not the actual error message
try:
a = int(input('Please enter the first number it must be >= 200' + '\n' + '(note the sum of both numbers must be <=300)'))
b= int(input('please your second number it must be <= 50'))
c = a + b
assert a >=200 and isinstance(a, int), 'invalid entry for a must be <= 200 and a number'
assert b<=50 and isinstance(b, int), 'invalid entry for b must be <=50 and a number'
assert c <= 300, 'The sum of your numbers is >300'
except ValueError
print('error')
else:
print('All in range')
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
添加一个除外的
,该 sossertionError 并打印出来:
Add an
except
that catches theAssertionError
and prints it: