Python程序求解二次方程的根

编写一个Python程序,用一个例子来查找二次方程的根。二次方程的数学表示是ax²+bx+c = 0。二次方程可以有两个根,它们完全取决于判别式。如果判别式> 0,则该方程有两个不同的实根

如果判别式= 0,则存在两个相等且实有的根。

如果判别式< 0,则存在两个不同的复根。

使用elif查找二次方程根的程序

这个Python程序允许用户输入a、b和c的三个值。通过使用这些值,此代码使用Elif语句查找二次方程的根。请参考Python

import math

a = int(input("Please Enter a Value of a Quadratic Equation : "))
b = int(input("Please Enter b Value of a Quadratic Equation : "))
c = int(input("Please Enter c Value of a Quadratic Equation : "))

discriminant = (b * b) - (4 * a * c)

if(discriminant > 0):
    root1 = (-b + math.sqrt(discriminant) / (2 * a))
    root2 = (-b - math.sqrt(discriminant) / (2 * a))
    print("Two Distinct Real Roots Exists: root1 = %.2f and root2 = %.2f" %(root1, root2))
elif(discriminant == 0):
    root1 = root2 = -b / (2 * a)
    print("Two Equal and Real Roots Exists: root1 = %.2f and root2 = %.2f" %(root1, root2))
elif(discriminant < 0):
    root1 = root2 = -b / (2 * a)
    imaginary = math.sqrt(-discriminant) / (2 * a)
    print("Two Distinct Complex Roots Exists: root1 = %.2f+%.2f and root2 = %.2f-%.2f" %(root1, imaginary, root2, imaginary))
Python Program to find roots of a Quadratic Equation