【发布时间】:2016-10-29 19:54:47
【问题描述】:
我正在尝试编写一个程序来检查 tries 到 max_tries 范围内的 Herone Triangle,但 math.sqrt() 让我头疼
这是我的代码
import math
max_tries = 10000
tries = 1
half_perimeter = ((tries * 3) + 3) / 2
for num in range(tries,max_try,1):
area = math.sqrt(half_perimeter * (half_perimeter - tries) * (half_perimeter - tries - 1) * (half_perimeter - tries - 2))
if isinstance(area, int ) == True:
print (tries)
tries = tries + 1
else :
tries = tries + 1
每当我运行时,我都会从 python 获得ValueError: math domain error。我的代码有什么问题?返回的整个错误是
Traceback (most recent call last):
File "C:\Users\phong\AppData\Local\Programs\Python\Python35\Herone Triangle.py", line 9, in <module>
area = math.sqrt(half_perimeter * (half_perimeter - tries) * (half_perimeter - tries - 1) * (half_perimeter - tries - 2))
ValueError: math domain error
我还是个脚本小子,刚接触 Python。谢谢大家
【问题讨论】:
-
你取的是负数的平方根。
-
您的代码没有定义变量
max_try,这使我们更难以运行您的代码或找到确切的问题。见How to create a Minimal, Complete, and Verifiable example。