patternpythonMinor
Simple Quadratic Equation Solver
Viewed 0 times
solverquadraticsimpleequation
Problem
Still pretty new to coding. I posted that Twin Primes Finder the other day and you guys were a great help so thanks. Here's my quadratic equation solver:
import math
#variables for initial equation
a = int(raw_input("Enter a:"))
b = int(raw_input("Enter b:"))
c = int(raw_input("Enter c:"))
#variables for quad. equation
negative_b = -b
b_squared = b**2
four_a_c = 4*a*c
determinant = b_squared - four_a_c
#prints initial equation
equation = [str(a)+"x^2", str(b)+"x", str(c)]
print "Solve: " + " + ".join(equation)
if determinant < 0:
print "No real solutions."
elif a == 0:
y_int = float(-c)/b
print "x =", y_int
print "But this is a straight line."
else:
root_determinant = math.sqrt(determinant)
two_a = 2*a
numerator_one = negative_b - root_determinant
answer_one = numerator_one/two_a
if determinant == 0:
print "x =", answer_one
else:
numerator_two = negative_b + root_determinant
answer_two = numerator_two/two_a
print "x =", answer_one, "or", answer_twoSolution
I wold separate the concerns somewhat and put them into functions.
One major concern is actually solving the quadratic equation. Another is the fancy input/output around it (if you want to be pedantic, that is actually two concerns).
The solving function, which I would call
I inlined some of the computations, if they were only used once.
Now, the
Finally, I would use
One major concern is actually solving the quadratic equation. Another is the fancy input/output around it (if you want to be pedantic, that is actually two concerns).
The solving function, which I would call
solve_quadratic returns a list of the solutions. This list can be empty (if there are no resolutions) or have multiple entries. We can use this fact to just append a solution if there are multiple.I inlined some of the computations, if they were only used once.
import math
def solve_quadratic(a, b, c):
"""
Given three real coefficients,
returns the (real) roots of the second degree polynomial
"""
negative_b = -b
determinant = b**2 - 4*a*c
if determinant < 0:
return []
elif a == 0:
return [float(-c)/b]
else:
root_determinant = math.sqrt(determinant)
two_a = 2*a
answers = [(negative_b - root_determinant)/two_a]
if determinant:
answers.append((negative_b + root_determinant)/two_a)
return answersNow, the
input part and the printing which serves as interpretation. For the initial printing I would use str.format, instead of first building a list and then joining it. It is slightly more readable IMO.Finally, I would use
if __name__ == "__main__": to be able to import these functions in another script without calling the input function.def fancy_quadratic_solver():
a = int(raw_input("Enter a:"))
b = int(raw_input("Enter b:"))
c = int(raw_input("Enter c:"))
print "Solve: {}x^2 + {}x + {}".format(a, b, c)
x = solve_quadratic(a, b, c)
if not x:
print "No real solutions."
else:
print "x =", " or ".join(map(str, x))
if a == 0:
print "But this is a straight line."
if __name__ == "__main__":
fancy_quadratic_solver()Code Snippets
import math
def solve_quadratic(a, b, c):
"""
Given three real coefficients,
returns the (real) roots of the second degree polynomial
"""
negative_b = -b
determinant = b**2 - 4*a*c
if determinant < 0:
return []
elif a == 0:
return [float(-c)/b]
else:
root_determinant = math.sqrt(determinant)
two_a = 2*a
answers = [(negative_b - root_determinant)/two_a]
if determinant:
answers.append((negative_b + root_determinant)/two_a)
return answersdef fancy_quadratic_solver():
a = int(raw_input("Enter a:"))
b = int(raw_input("Enter b:"))
c = int(raw_input("Enter c:"))
print "Solve: {}x^2 + {}x + {}".format(a, b, c)
x = solve_quadratic(a, b, c)
if not x:
print "No real solutions."
else:
print "x =", " or ".join(map(str, x))
if a == 0:
print "But this is a straight line."
if __name__ == "__main__":
fancy_quadratic_solver()Context
StackExchange Code Review Q#154918, answer score: 3
Revisions (0)
No revisions yet.