【发布时间】:2016-09-17 01:53:24
【问题描述】:
找出除了最后一个错误之外的错误,我现在收到此错误消息,但不知道为什么,我正在使用我的老师给我们使用的 x1 和 x2 的确切公式,我正在无法找出错误。
# Quadratic Formula
# Import the math library to use sqrt() function to find the square root
import math
print("This equation solves for x in the binomial equation: ax^2 + bx + c = 0")
# Get the equation's coefficients and constant from the user
a = 0
while a == 0:
try:
a = float(input("Enter the first coefficeint, or a, value: "))
if a == 0:
raise ValueError
except ValueError:
print("The value you entered is invalid. Zero is not allowed")
else:
break
while (True):
try:
b = float(input("Enter the Second coefficeint, or b, value: "))
except ValueError:
print("The value you entered is invalid. only real numbers")
else:
break
while (True):
try:
c = float(input("Enter the last coefficeint, or c, value: "))
except ValueError:
print("The value you entered is invalid. only real numbers")
else:
break
d = (b**2) - (4*a*c)
x1 = ((-b) + math.sqrt(b**2 - 4*a*c)) / (2*a)
x2 = ((-b) - math.sqrt(b**2 - 4*a*c)) / (2*a)
print("X is: ", x1, " or ", x2)
do_calculation = True
while(do_calculation):
another_calculation = input("Do you want to perform another calculation? (y/n):")
if(another_calculation !="y"):
此方程求解二项式方程中的 x:ax^2 + bx + c = 0 输入第一个 coefficeint 或 a 值:2 输入第二个系数,或 b,值:3 输入最后一个 coefficeint 或 c 值:4 回溯(最近一次通话最后): 文件“/Users/cadenhastie/Downloads/Jtwyp6QuadraticEqnCalc/improvedquadraticeqncalc.py”,第 34 行,在 x1 = ((-b) + math.sqrt(b**2 - 4*a*c)) / (2*a) ValueError:数学域错误
【问题讨论】:
-
请使用完整的错误堆栈跟踪进行编辑
-
修复你的缩进,这可能会解决问题。
-
在进行计算之前检查
d是否为正。如果 d 为负数,请考虑抛物线的含义。 -
想通了,谢谢!