什么是我的Python代码运行?
问题描述:
我的代码是给我一些错误 回溯(最近通话最后一个): 文件“蟒蛇”,7号线,在 ValueError异常:数学域错误什么是我的Python代码运行?
import math
a= 3
b= 5
c= 2
d= b^2 -4*a*c
x1 = math.sqrt(d)
print(x1)
答
d
为负时,有没有真正的解决方案,因此它的平方ROOR也没有真正:
还请注意,b^2
是不是B squared
,这是b xor 2
。为b square
,使用b**2
,或b*b
import math
a = 3
b = 5
c = 2
d = b**2 - 4*a*c # Attention, b^2 is not b square, use b**2
if d > 0:
x1 = math.sqrt(d)
print(x1)
else:
print("there are no real roots")
答
我觉得'b^2'也许应该是'b ** 2' –
''^是按位异或,你想用''**乘方。 –
[ValueError:数学领域错误]的可能重复(https://stackoverflow.com/questions/15890503/valueerror-math-domain-error) –