0

Figured out the errors except for this last one, Im now getting this error message, and can't figure out why, I'm using the exact formulas for x1 and x2 that my teacher gave us to use and i'm not able to figure the error out.

    # Quadratic Formula

# Import the math library to use sqrt() function to find the square root
import math

print("This equation solves for x in the binomial equation: ax^2 + bx + c = 0")

# Get the equation's coefficients and constant from the user
a = 0
while a == 0:
    try:
        a = float(input("Enter the first coefficeint, or a, value:  "))
        if a == 0:
           raise ValueError
    except ValueError:
        print("The value you entered is invalid. Zero is not allowed")
    else:
            break
while (True):
    try:
        b = float(input("Enter the Second coefficeint, or b, value:  "))
    except ValueError:
            print("The value you entered is invalid. only real numbers")
    else:
            break
while (True):
    try:
        c = float(input("Enter the last coefficeint, or c, value:  "))
    except ValueError:
            print("The value you entered is invalid. only real numbers")
    else:
            break
d = (b**2) - (4*a*c)
x1 = ((-b) + math.sqrt(b**2 - 4*a*c)) / (2*a) 
x2 = ((-b) - math.sqrt(b**2 - 4*a*c)) / (2*a)
print("X is: ", x1, " or ", x2)
do_calculation = True
while(do_calculation):
         another_calculation = input("Do you want to perform another calculation? (y/n):")
if(another_calculation !="y"):

This equation solves for x in the binomial equation: ax^2 + bx + c = 0 Enter the first coefficeint, or a, value: 2 Enter the Second coefficeint, or b, value: 3 Enter the last coefficeint, or c, value: 4 Traceback (most recent call last): File "/Users/cadenhastie/Downloads/Jtwyp6QuadraticEqnCalc/improvedquadraticeqncalc.py", line 34, in x1 = ((-b) + math.sqrt(b**2 - 4*a*c)) / (2*a) ValueError: math domain error

1 Answers1

0

You have try statements with no corresponding except statement. You should generally avoid while True:. The indentation in your code has many issues

To get one value from the user with error handling, you could do something like the code below. You would then repeat this for each coefficient you want the user to enter. You probably want to wrap this in a function at some point so you are not writing duplicate code.

a = 0 while a == 0: try: a = float(input("Enter the first coefficeint, or a, value: ")) if a == 0: raise ValueError except ValueError: print("The value you entered is invalid. Zero is not allowed")

Brad Campbell
  • 2,969
  • 2
  • 23
  • 21
  • Thanks so much, Very new to this and its an online class so trying to teach myself as best as I can. That helped a lot and got me to better understand errors afterward and now program is running, just a few minor flaws thanks! – Konabluest Sep 16 '16 at 17:25
  • Sorry that came across rather bluntly. Just try to break your code into small pieces and get each piece to work then put them together. You'll get there! – Brad Campbell Sep 16 '16 at 17:36
  • No problem, I know its hard to explain simple things without coming across that way, I worked out the syntax errors and it runs but still having problems getting the program to ask for the second and third coefficient for some reason. Back to youtube I go! – Konabluest Sep 16 '16 at 17:38
  • Any help on this problem, once this is fixed the program will be finished after hours upon hours of working on it. – Konabluest Sep 16 '16 at 17:53