【问题标题】:What is a ValueError什么是 ValueError
【发布时间】:2018-12-05 04:02:05
【问题描述】:
我找到了以下定义in the docs,但我仍然不清楚:
当内置操作或函数接收到类型正确但值不合适的参数时引发
你有例子吗?
【问题讨论】:
标签:
python
python-3.x
valueerror
【解决方案1】:
看看math#sqrt - 它需要一个数字,但这个数字必须是非负数。如果你尝试用负数调用它(这不能在实数数学中完成),你会得到一个ValueError:
>>> from math import sqrt
>>> sqrt(-1)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: math domain error
【解决方案2】:
给出具体ValueError 的示例如下:
import
math.log(0)
回馈:
Traceback (most recent call last):
File "<ipython-input-6-f7278b7c2ed1>", line 1, in <module>
math.log(0)
ValueError: math domain error
许多其他数学函数在输入错误时会产生相同的结果(math.sqrt(-1)、math.ceil(math.nan) 等等...)