Quadratic formula solver in python
<p>I'm somewhat new to python, but I'm trying my best to learn. My code is</p>

<pre><code>import math

a = 5
b = 5
c = 5

def quad_solve(a, b, c):
    q1 = b*b
    q2 = 4*a*c
    q3 = 2*a
    q4 = q1-q2
    sqr = math.sqrt(q4)
    sol1p1 = b+sqr
    sol1p2 = sol1p1/2
    sol2p1 = b-sqr
    sol2p2 = sol2p1/2
    print ("(",sol1p2,",",sol2p2,")")

quad_solve(a, b, c)
</code></pre>

<p>And when I run it it gives the error </p>

<pre><code>Traceback (most recent call last):
  File "python", line 19, in &lt;module&gt;
  File "python", line 12, in quad_solve
ValueError: math domain error
</code></pre>

<p>which I don't really understand. </p>

<p>I'm trying to create a quadratic formula solver. I use the math module, and then define three variables, a, b, and c. Then, I define a function that takes in those variables (I call the function at the end). In the function, I define four quantities. <code>q1</code> is the b squared under the square root, <code>q2</code> is the 4ac also under the square root, <code>q3</code> is the denominator, and <code>q4</code> calculates the total under the square root (i.e., <code>q1</code> - <code>q2</code>). Then, I define a variable called <code>sqr</code> which is equal to the square root of <code>q4</code>. Then, I define four more variables, which calculate the solutions. <code>sol1p1</code> takes b + sqr, and <code>sol1p2</code> takes <code>sol1p1</code> and divides it by two. This gives the first solution. Then, <code>sol2p1</code> takes b - sqr, and <code>sol2p2</code> takes <code>sol2p1</code> and divides it by two. Finally, <code>sol1p2</code> and <code>sol2p2</code> are printed, in a set of parentheses with a comma between. I hope that makes sense; if any clarification is needed about the variable names, please let me know. </p>

<p>I am using the online compiler repl.it (I don't know if there's anything special to consider with that).</p>

<p>Thanks!</p>

<hr>

<p><strong>Edit:</strong></p>

<p>I updated my code, per Code Apprentice's recommendations. I started by adding an if statement:</p>

<pre><code>import math

a = 5
b = 5
c = 5

def quad_solve(a, b, c):
    q1 = b*b
    q2 = 4*a*c
    q3 = 2*a
    q4 = q1-q2
    check = math.tan(q2)
    if (q1 &gt; check):
        sqr = math.sqrt(q4)
        sol1p1 = b+sqr
        sol1p2 = sol1p1/2
        sol2p1 = b-sqr
        sol2p2 = sol2p1/2
        print ("(",sol1p2,",",sol2p2,")")
    else:
        print "Imaginary number. There are no zeros."

    quad_solve(a, b, c)
</code></pre>

<p>but it is continuing to return the error </p>

<pre><code>Traceback (most recent call last):
  File "python", line 23, in &lt;module&gt;
  File "python", line 14, in quad_solve
ValueError: math domain error
</code></pre>

<p>I'm not sure why. </p>

<p>b^2 has to be greater than 4ac, So right now, that <code>sqrt()</code> function is getting a negative number. </p>

python math compiler-errors