Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我的代码给了我一些错误 Traceback (最近一次调用最后): File "python", line 7, in ValueError: math domain error
import math a= 3 b= 5 c= 2 d= b^2 -4*a*c x1 = math.sqrt(d) print(x1)
你d的是-17(你很可能想使用**而不是^)
d
-17
**
^
负数的根是什么?
这就是异常所说的
d当没有实解时为负,因此它的平方也不是实数: 还请注意b^2不是 b squared,它是b xor 2。为b square、使用b**2或b*b
b^2
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")