python脚本中的数学语法错误

时间:2018-04-28 22:40:30

标签: python

Hello Stack Overflow社区! 当我尝试运行这个python脚本(其目的是在二次方程中求解x)时,它返回以下消息:

Traceback (most recent call last):
    File "/private/var/mobile/Containers/Shared/AppGroup/C692DE78-B0DD-44FF-9815-4675E6956B0A/Pythonista3/Documents/quadratic solver.py", line 13, in <module>
    root_b_squared_minus_four_a_c = sqrt(b_squared_minus_four_a_c)
ValueError: math domain error

以下是我正在运行的代码:

from math import *
# Set up Variables A, B, and C, by asking for input
a=int(input("Please enter an a value: "))
b=int(input("Please enter a b value: "))
c=int(input("Please enter a c value: "))
#Set up variables for later use, ac, 4ac, and 2a, in the quadratic formula
ac = a*c
four_ac = 4 * ac
two_a = 2*a
b_squared_minus_four_a_c = int(b*b - four_ac)
minus_b = -b
answer_one_step_one = 0
root_b_squared_minus_four_a_c = sqrt(b_squared_minus_four_a_c)
#This section is the solution to the quadratic if a is equal to one, to prevent against printing 1 in front of x

if a == 1:
     print("solving equation x^2+" + str(b) + "x+" + str(c))
     answer_one_step_one = minus_b + root_b_squared_minus_four_a_c
     answer_one = answer_one_step_one / two_a

     answer_two_step_one = minus_b - root_b_squared_minus_four_a_c
     answer_two = answer_two_step_one / two_a
     print("The two solutions for x are:" + str(answer_one) + ",and " + str(answer_two))

else:
    print("Solving equation" + str(a)+ "x^2+" + str(b) + "x+" + str(c))
    answer_one_step_one = minus_b + sqrt(b_squared_minus_four_a_c)
    answer_one = answer_one_step_one / two_a

    answer_two_step_one = minus_b - sqrt(b_squared_minus_four_a_c)
    answer_two = answer_two_step_one / two_a
    print("The two solutions for x are:" + str(answer_one) + ",and " + str(answer_two))

我该如何解决这个问题?

谢谢!

标记

3 个答案:

答案 0 :(得分:1)

您收到此错误是因为您传递的是sqrt()函数的负数。为了避免这种情况,当b ^ 2 - 4ac为负时验证,因为只有当它大于或等于0时才能获得所述结果的平方根。

答案 1 :(得分:0)

“ValueError:math domain error”在这种情况下意味着您将负数传递给平方根计算。它不能那样做,因此错误就是结果。

您需要重新组织代码以首先检查这种可能性。并非每个二次方程都有真正的解。

答案 2 :(得分:0)

正如其他一些评论所指出的那样,错误是由你取负数的平方根引起的。如果您在检查此项的所有其他内容之前添加if条件,则应解决您的问题:

#First check if there are any real solutions?
if b_squared_minus_four_a_c < 0:
    print('No real solutions')

elif a == 1:
    print("solving equation x^2+" + str(b) + "x+" + str(c))
    answer_one_step_one = minus_b + root_b_squared_minus_four_a_c
    answer_one = answer_one_step_one / two_a

    answer_two_step_one = minus_b - root_b_squared_minus_four_a_c
    answer_two = answer_two_step_one / two_a
    print("The two solutions for x are:" + str(answer_one) + ",and "     + 
    str(answer_two))

else:
    print("Solving equation" + str(a)+ "x^2+" + str(b) + "x+" + str(c))
    answer_one_step_one = minus_b + sqrt(b_squared_minus_four_a_c)
    answer_one = answer_one_step_one / two_a

    answer_two_step_one = minus_b - sqrt(b_squared_minus_four_a_c)
    answer_two = answer_two_step_one / two_a
    print("The two solutions for x are:" + str(answer_one) + ",and " +         
    str(answer_two))