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:"))

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
if determinant == 0:
else:
numerator_two = negative_b + root_determinant

• You title sounds like this is a follow up question, in which case you should add a link to the previous editions. But it is not, it is just your third question here. Therefore I edited the title :) – Graipher Feb 9 '17 at 14:47
• Do you care about numerical stability? – harold Feb 9 '17 at 17:52
• What's that harold? – cornelius Feb 10 '17 at 1:08

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 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

"""
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
if determinant:


Now, 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)