求二次方程

时间:2013-03-13 23:23:37

标签: python python-2.7

我的计划似乎没有给我正确的解决方案。有时确实如此,有时却没有。我找不到我的错误。有什么建议吗?

import math

a,b,c = input("Enter the coefficients of a, b and c separated by commas: ")

d = b**2-4*a*c # discriminant

if d < 0:
    print "This equation has no real solution"
elif d == 0:
    x = (-b+math.sqrt(b**2-4*a*c))/2*a
    print "This equation has one solutions: ", x
else:
    x1 = (-b+math.sqrt(b**2-4*a*c))/2*a
    x2 = (-b-math.sqrt(b**2-4*a*c))/2*a
    print "This equation has two solutions: ", x1, " and", x2

9 个答案:

答案 0 :(得分:17)

此行导致问题:

(-b+math.sqrt(b**2-4*a*c))/2*a

x/2*a被解释为(x/2)*a。您需要更多括号:

(-b + math.sqrt(b**2 - 4*a*c)) / (2 * a)

另外,如果您已经存储d,为什么不使用它?

x = (-b + math.sqrt(d)) / (2 * a)

答案 1 :(得分:5)

在这里,你应该每次给你正确答案!

a = int(input("Enter the coefficients of a: "))
b = int(input("Enter the coefficients of b: "))
c = int(input("Enter the coefficients of c: "))

d = b**2-4*a*c # discriminant

if d < 0:
    print ("This equation has no real solution")
elif d == 0:
    x = (-b+math.sqrt(b**2-4*a*c))/2*a
    print ("This equation has one solutions: "), x
else:
    x1 = (-b+math.sqrt((b**2)-(4*(a*c))))/(2*a)
    x2 = (-b-math.sqrt((b**2)-(4*(a*c))))/(2*a)
    print ("This equation has two solutions: ", x1, " or", x2)

答案 2 :(得分:1)

# syntaxis:2.7
# solution for quadratic equation
# a*x**2 + b*x + c = 0

d = b**2-4*a*c # discriminant

if d < 0:
    print 'No solutions'
elif d == 0:
    x1 = -b / (2*a)
    print 'The sole solution is',x1
else: # if d > 0
    x1 = (-b + math.sqrt(d)) / (2*a)
    x2 = (-b - math.sqrt(d)) / (2*a)
    print 'Solutions are',x1,'and',x2

答案 3 :(得分:1)

如何接受复杂的根作为解决方案?

import math

# User inserting the values of a, b and c

a = float(input("Insert coefficient a: "))
b = float(input("Insert coefficient b: "))
c = float(input("Insert coefficient c: "))

discriminant = b**2 - 4 * a * c

if discriminant >= 0:
    x_1=(-b+math.sqrt(discriminant))/2*a
    x_2=(-b-math.sqrt(discriminant))/2*a
else:
    x_1= complex((-b/(2*a)),math.sqrt(-discriminant)/(2*a))
    x_2= complex((-b/(2*a)),-math.sqrt(-discriminant)/(2*a))

if discriminant > 0:
    print("The function has two distinct real roots: ", x_1, " and ", x_2)
elif discriminant == 0:
    print("The function has one double root: ", x_1)
else:
    print("The function has two complex (conjugate) roots: ", x_1, " and ", x_2)

答案 4 :(得分:0)

\1 = \$this->\1

答案 5 :(得分:0)

通过键盘输入

a=float(input("enter the 1st number : "))
b=float(input("enter the 2nd number : "))
c=float(input("enter the 3rd number : "))

计算判别力

d = (b**2) - (4*a*c)

可能的解决方案是

sol_1 = (-b-(0.5**d))/(2*a)
sol_2 = (-b+(0.5**d))/(2*a)

打印结果

print('The solution are  %0.f,%0.f'%(sol_1,sol_2))

答案 6 :(得分:0)

下面是求解二次方程的程序。

例如:求解x2 + 3x – 4 = 0

这种二次发生是因素:

x2 + 3x – 4 =(x + 4)(x – 1)= 0

我们已经知道解为x = –4和x = 1。

enter image description here

    # import complex math module
    import cmath

    a = 1
    b = 5
    c = 6

    # To take coefficient input from the users
    # a = float(input('Enter a: '))
    # b = float(input('Enter b: '))
    # c = float(input('Enter c: '))

    # calculate the discriminant
    d = (b**2) - (4*a*c)

    # find two solutions
    sol1 = (-b-cmath.sqrt(d))/(2*a)
    sol2 = (-b+cmath.sqrt(d))/(2*a)

    print('The solution are {0} and {1}'.format(sol1,sol2))

来源:Python Program to Solve Quadratic Equation

答案 7 :(得分:0)

一个班轮求解二次方程式

from math import sqrt
s = lambda a,b,c: {(-b-sqrt(d))/2*a,(-b+sqrt(d))/2*a} if (d:=b**2-4*a*c)>=0 else {}
roots_set = s(int(input('a=')),int(input('b=')),int(input('c=')))
print(roots_set,f'number of roots {len(roots_set)}')

one line python solve quadratic equations video

答案 8 :(得分:-5)

<code>
import cmath
import math
print(" we are going to programming second grade equation in python")
print(" a^2 x + b x + c =0")

num1 = int(input(" enter A please : "))
num2 = int(input(" enter B please : "))
num3 = int(input(" enter c please : "))
v = num2*num2 - 4 *num1 * num3
print(v)
if v < 0 :
    print("wrong values")
else:
    print("root of delta =", v)
    k= math.sqrt(v)

def two_sol(x,y) :
    x_f= (-y + v)/(4*x)
    x_s =(-y - v)/(4*x)
    return x_f , x_s

def one_sol(x):
    x_f = (-y + v) / (4 * x)

if v >0 :
    print("we have two solution :" ,two_sol(num1,num2)) 
elif v == 0:
    print( "we have one solution :" , one_sol(y)) 
else:
    print(" there is no solution !!")
</code>