Jul 2, 2017 01:12 PM
(This post was last modified: Jul 2, 2017 01:40 PM by Gerymc.)
I am trying to make a program in which you enter the A, B, and C variables in the quadratic formula and it find's X's two values. I already set up an if-else statement so that if the discriminant is negative, it will tell the user there are no solutions, however I am having trouble on the actual math part.
The quadratic formula is x = -b±√b^2-4ac all divided by 2a.
Part of my code is below. As you can see in the else statement, I set up two variables because X can have two different answers because there is the plus-minus sign. Would the way I did it be the correct way to do it? When I try to test it out, it gives the wrong answers so my math is wrong somehow.
EDIT: Realized that I need to make the formula like this:
The quadratic formula is x = -b±√b^2-4ac all divided by 2a.
Part of my code is below. As you can see in the else statement, I set up two variables because X can have two different answers because there is the plus-minus sign. Would the way I did it be the correct way to do it? When I try to test it out, it gives the wrong answers so my math is wrong somehow.
Code
#Makes a variable for the discriminant
disc = b**2 - 4*a*c
#If the discriminant is less than 0, it says no real solutions
if disc < 0:
print "No real solutions"
#If the discriminant is 0, it finds the 1 value of X
elif disc == 0:
x = -b+((b**2 - 4*a*c)**.5)/2*a
print "x = " + str(x)
else:
x1 = -b+((b**2 - 4*a*c)**.5)/2*a
x2 = -b-((b**2 - 4*a*c)**.5)/2*a
print "x = " + str(x1) + str(x2)EDIT: Realized that I need to make the formula like this:
Code
(-b+((b**2 - 4*a*c)**.5))/2*a

