Python Quadratic Equation
A quadratic equation has the form: ax2+bx+c=0.
Its solutions can be determined by computing the discriminant
Delta=b2-4ac:- If both the
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
:
(
"
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
(
)
:
(
"
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
:
(
"
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
)
(
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
- a
)
root2
=
(
-b -
math
.
sqrt
(
delta
)
)
/
(
2
- a
)
return
f
"The equation has two real roots: x1 = {root1}, x2 = {root2}"
elif
delta
==
0
:
One real root
root
=
-b /
(
2
- a
)
return
f
"The equation has one double real root: x = {root}"
else
:
Two complex roots
root1
=
(
-b +
cmath
.
sqrt
(
delta
)
)
/
(
2
- a
)
root2
=
(
-b -
cmath
.
sqrt
(
delta
)
)
/
(
2
- a
)
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
)
(
result
)
Python3 Examples