Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Python Quadratic Equation

Python Quadratic Equation

Document Object Reference Manual Python3 Examples

A quadratic equation has the form: ax2+bx+c=0.

Its solutions can be determined by computing the discriminant Δ=b2−4ac\Delta = b^2 - 4ac

Delta=b2-4ac:
  1. If both the
Δ>0 \Delta > 0 Delta>0, the equation has two real roots. 2. If Δ=0\Delta = 0 Delta=0, the equation has one real root (a double root). 3. If Δ<0\Delta < 0 Δ<0, the equation has no real roots but two complex roots.

The following example takes numbers from the user and solves a quadratic equation:

Example (Python 3.0+)

Filename: quadratic_solver.py

Author: www.runoob.com (optimized by ChatGPT)

Program function: solve the quadratic equation ax**2 + bx + c = 0

Note: a != 0; a, b and c are real numbers entered by the user

import

cmath

Import the cmath module to support complex arithmetic

def

get_float_input

(

prompt

)

:

"""

Get a float entered by the user and handle invalid input. :param prompt: the prompt message :return: the float entered by the user

"""

while

True

:

try

:

return

float

(

input

(

prompt

)

)

except

ValueError

:

print

(

"

Please enter a valid number!

"

)

def

solve_quadratic

(

a

,

b

,

c

)

:

"""

Compute the solutions of a quadratic equation. :param a: quadratic coefficient :param b: linear coefficient :param c: constant term :return: the two solutions of the quadratic equation

"""

discriminant

=

b

**

2

4

a

c

Compute the discriminant

root1

=

(

b

cmath

.

sqrt

(

discriminant

)

)

/

(

2

a

)

root2

=

(

b

cmath

.

sqrt

(

discriminant

)

)

/

(

2

a

)

return

root1

,

root2

def

main

(

)

:

print

(

"

Solve the quadratic equation ax^2 + bx + c = 0

"

)

Get user input

a

=

get_float_input

(

"

Please enter the quadratic coefficient a (a != 0):

"

)

while

a

==

0

:

print

(

"

The quadratic coefficient a of a quadratic equation cannot be 0!

"

)

a

=

get_float_input

(

"

Please re-enter the quadratic coefficient a (a != 0):

"

)

b

=

get_float_input

(

"

Please enter the linear coefficient b:

"

)

c

=

get_float_input

(

"

Please enter the constant term c:

"

)

Compute and output the result

root1

,

root2

=

solve_quadratic

(

a

,

b

,

c

)

print

(

f

"

The solutions of the equation are: {root1} and {root2}

"

)

if

name

==

"

main

"

:

main

(

)

The output of the code above is:


求解二次方程 ax^2 + bx + c = 0
请输入二次项系数 a(a ≠ 0):1
请输入一次项系数 b:5
请输入常数项 c:6
方程的解为:(-3+0j) 和 (-2+0j)

In this example we use the sqrt() method of the cmath (complex math) module to compute square roots.

Example 2

import

math

import

cmath

def

solve_quadratic

(

a

,

b

,

c

)

:

   

"""
    Solve the quadratic equation ax^2 + bx + c = 0
    :param a: quadratic coefficient
    :param b: linear coefficient
    :param c: constant term
    :return: the solutions of the equation (may be real or complex)
    """

   

if

a

==

0

:

       

Handling non-quadratic equations

       

if

b

==

0

:

           

return

"No solution"

if

c

!=

0

else

"The equation has infinitely many solutions"

       

return

f

"The equation is linear, with solution x = {-c / b}"

   

   

Compute the discriminant

    delta

=

b**

2

4

ac

   

   

if

delta

0

:

       

Two real roots

        root1

=

(

-b +

math

.

sqrt

(

delta

)

)

/

(

2

)

        root2

=

(

-b -

math

.

sqrt

(

delta

)

)

/

(

2

)

       

return

f

"The equation has two real roots: x1 = {root1}, x2 = {root2}"

   

elif

delta

==

0

:

       

One real root

        root

=

-b /

(

2

)

       

return

f

"The equation has one double real root: x = {root}"

   

else

:

       

Two complex roots

        root1

=

(

-b +

cmath

.

sqrt

(

delta

)

)

/

(

2

)

        root2

=

(

-b -

cmath

.

sqrt

(

delta

)

)

/

(

2

)

       

return

f

"The equation has two complex roots: x1 = {root1}, x2 = {root2}"

Example call

a

,

b

,

c

=

1

,

3

,

2

result

=

solve_quadratic

(

a

,

b

,

c

)

print

(

result

)

Document Object Reference Manual Python3 Examples