7
\$\begingroup\$

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_two 
\$\endgroup\$
3
  • 1
    \$\begingroup\$ 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 :) \$\endgroup\$ Commented Feb 9, 2017 at 14:47
  • \$\begingroup\$ Do you care about numerical stability? \$\endgroup\$ Commented Feb 9, 2017 at 17:52
  • \$\begingroup\$ What's that harold? \$\endgroup\$ Commented Feb 10, 2017 at 1:08

1 Answer 1

3
\$\begingroup\$

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

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) 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() 
\$\endgroup\$
2
  • \$\begingroup\$ Wouldn't it be more appropriate to separate user input and function input, asking for user input out side of the function? That way if you get input from somewhere else you can just call the function. \$\endgroup\$ Commented Feb 10, 2017 at 8:02
  • \$\begingroup\$ @ChatterOne I was considering that as well. But in my opinion the solver itself is the important part, and this one you can call with any input. The rest is just interface. \$\endgroup\$ Commented Feb 10, 2017 at 17:07

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.