admin 管理员组文章数量: 1086019
Edit - here's the actual answer which I got with help from other students:
try:
user_num = int(input())
div_num = int(input())
quotient = int(user_num / div_num)
print(quotient)
except ValueError as value_error:
print(f'Input Exception: {value_error}')
except ZeroDivisionError as zero_error:
print(f'Zero Division Exception: integer division or modulo by zero')
Working on a class for intro to python, I have to get this specific lab done to 100% completion and I'm missing 1 point.
in my code we're tasked to.. Write a program that reads integers user_num and div_num as input, and output the quotient (user_num divided by div_num). Use a try block to perform all the statements. Use an except block to catch any ZeroDivisionError and output an exception message. Use another except block to catch any ValueError caused by invalid input and output an exception message. Note: ZeroDivisionError is thrown when a division by zero happens. ValueError is thrown when a user enters a value of different data type than what is defined in the program. Do not include code to throw any exception in the program.
try:
user_num = input()
div_num = input()
user_num = int(user_num)
div_num = int(div_num)
quotient = user_num // div_num
print(quotient)
except ZeroDivisionError:
if div_num == 0:
print("Zero Division Exception: integer division or modulo by zero")
except ValueError:
if user_num is not int:
print(f"Input Exception: invalid literal for int() with base 10: '{user_num}'")
except ValueError:
if div_num is not int:
print(f"Input Exception: invalid literal for int() with base 10: '{div_num}'")
Everything is working properly except the last final test that I need to get it through. With an user_num of 25 and div_num of 0.5 it SHOULD run the 2nd ValueError and not the 1st value error. When this code is ran it spits out Input Exception: invalid literal for int() with base 10: '25' Which to me doesn't make sense, I'm not sure how or what I can do to get the FIRST ValueError exception to either skip or pass so the actual 2nd ValueError can go through. Let it be noted that if the first input was a decimal it would still spit ONE error and one error only. Why is it stating the 25 is wrong when it's in fact a WHOLE number and not a decimal?
本文标签: python 3xAn exception is correct but prints the wrong one (answered in edit)Stack Overflow
版权声明:本文标题:python 3.x - An exception is correct, but prints the wrong one? (answered in edit) - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.roclinux.cn/p/1743998041a2515936.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论