python脚本中的数学语法错误

2024-04-25 21:29:47 发布

您现在位置:Python中文网/ 问答频道 /正文

你好,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))

我该怎么解决这个问题

谢谢

标记


Tags: andanswerforinputsteprootsqrtone
3条回答

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

您需要先重新组织代码以检查这种可能性。不是每个二次方程都有实解

正如其他一些注释所指出的,这个错误是由取负数的平方根引起的。如果在检查此问题的所有其他条件之前添加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))

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

相关问题 更多 >