有人能告诉我为什么会出现此错误?ValueError:二级公式中的数学域错误
Traceback (most recent call last):
File "file.py", line 14, in <module>
p2 = math.sqrt(b*b -4*a*c)
ValueError: math domain error
我是个新手编码,所以需要一些帮助:)
我的代码如下所示:
# -*- coding: utf-8 -*-
a = input("¿Qué valor es a?")
while(str(a).isdigit() != True):
a = input("Porfavor, introduce un número para a, no un texto")
b = input("¿Qué valor es b?")
while(str(b).isdigit() != True):
b = input("Porfavor, introduce un número para b, no un texto")
c = input("¿Qué valor es c?")
while(str(c).isdigit() != True):
c = input("Porfavor, introduce un número para c, no un texto")
import math
p1 = b * -1
p2 = math.sqrt(b*b -4*a*c)
p3 = 2*a
s1 = (p1+p2)/p3
s2 = (p1-p2)/p3
print("Soluciones :", s1, " y ", s2)
'math.sqrt'不处理负值... –
您确定执行时输入的参数a,b和c是否为有效解决方案? 如果'4 * a * c> b * b'没有二次方程的解。你应该添加一个检查IF(4 * a * c> b * b):print(“没有解决方案”)否则:...' – trashy
@trashy如果您的域名是复数,肯定有解决方案。 –