Incorrect answers for quadratic equations

I was wondering if anyone could tell me why my python code for solving quadratic equations does not work. I looked through it and found no errors.

print("This program will solve quadratic equations for you") print("It uses the system 'ax**2 + bx + c'") print("a, b and c are all numbers with or without decimal \ points") print("Firstly, what is the value of a?") a = float(input("\n\nType in the coefficient of x squared")) b = float(input("\n\nNow for b. Type in the coefficient of x")) c = float(input("\n\nGreat. now what is the c value? The number alone?")) print("The first value for x is " ,(-b+(((b**2)-(4*a* c))* * 0.5)/(2*a))) print("\n\nThe second value for x is " ,(-b-(((b * * 2)-(4*a*c))** 0.5)/(2*a))) 

When a = 1 b = -4 and c = -3, I expect -1 and 4, but get 5.5 and 0.5

+5
source share
1 answer

Your problem is in the part that is trying to fulfill the quadratic formula:

 (-b+(((b**2)-(4*a* c))* * 0.5)/2*a) 

The problem is that * has the same priority as / , so you divide by 2 and then multiply by a . Also your parentheses are off, so I reduced the unnecessary and moved the wrong ones. In short, -b did not connect to the square root before division. Do you want to:

 (-b+(b**2-4*a*c)**0.5)/(2*a) 

PS To ask questions, it would be better to ask in the form of something like:

 >>> a = 2 >>> b = 1 >>> c = 3 >>> (-b+(((b**2)-(4*a* c))* * 0.5)/2*a) got blah, expected blam 

Since other printing and typing is not to blame (that you should be able to work quite easily).

+9
source

All Articles