Python异常ValueError值错误是我们开发过程中经常遇到的错误类型,下面就通过几种不同的应用场景来和大家解释一下如何认识错误异常提示和解决办法。
首先看看两组代码和错误提示:
1.场景1
import math
print(math.factorial(-5))
提示:
Traceback (most recent call last):
File "C:/Users/Administrator/Desktop/wakey/app/apps/tests.py", line 6, in <module>
print(math.factorial(-5))
ValueError: factorial() not defined for negative values
Process finished with exit code 1
2.场景2
str = input() # 输入特殊符号或者非数字
print(int(str))
提示:
*
Traceback (most recent call last):
File "C:/Users/Administrator/Desktop/wakey/app/apps/tests.py", line 11, in <module>
print(int(str))
ValueError: invalid literal for int() with base 10: '*'
Process finished with exit code 1
从上面两种错误来看,虽然错误类型一样,但是提示信息还是有一些不同的,这种不同主要是IDE对于代码识别的时候给出的智能提示,下面是我截图进行拆分错误提示的信息。