Hack Forums
[Python] Help making quadratic formula in python - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [Python] Help making quadratic formula in python (/showthread.php?tid=5660505)



Help making quadratic formula in python - Gerymc - Jul 2, 2017

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.

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



* - slober - Jul 2, 2017

You can write it a bit shorter if you wanted to.
ex.
Code
import cmath

delta = (b**2) - (4*a*c)
solution1 = (-b-cmath.sqrt(delta))/(2*a)
solution2 = (-b+cmath.sqrt(delta))/(2*a)



* - Gerymc - Jul 2, 2017

(Jul 2, 2017 01:37 PM)slober Wrote:  You can write it a bit shorter if you wanted to.
ex.
Code
import cmath

delta = (b**2) - (4*a*c)
solution1 = (-b-cmath.sqrt(delta))/(2*a)
solution2 = (-b+cmath.sqrt(delta))/(2*a)

I'm doing this for a class and they didn't teach us that yet, so I can't use it.


* - slober - Jul 2, 2017

(Jul 2, 2017 01:41 PM)jdugerymc Wrote:  I'm doing this for a class and they didn't teach us that yet, so I can't use it.

This would be my full solution
Code
import cmath

print('Solve the quadratic equation: ax**2 + bx + c = 0')
a = float(input('Please enter a : '))
b = float(input('Please enter b : '))
c = float(input('Please enter c : '))
delta = (b**2) - (4*a*c)

if delta < 0:
    print ("No real solutions")
elif delta == 0:
    solution1 = (-b+cmath.sqrt(delta))/(2*a)
    print ('x =',solution1)
else:
    solution1 = (-b-cmath.sqrt(delta))/(2*a)
    solution2 = (-b+cmath.sqrt(delta))/(2*a)

    print ('x =',solution1)
    print('x =',solution2)

or
Code
print ('x1 = {0} and x2 {1}'.format(solution1,solution2))
What kind of class are you following?


* - Gerymc - Jul 2, 2017

(Jul 2, 2017 01:54 PM)slober Wrote:  This would be my full solution
Code
import cmath

print('Solve the quadratic equation: ax**2 + bx + c = 0')
a = float(input('Please enter a : '))
b = float(input('Please enter b : '))
c = float(input('Please enter c : '))
delta = (b**2) - (4*a*c)

if delta < 0:
    print ("No real solutions")
elif delta == 0:
    solution1 = (-b+cmath.sqrt(delta))/(2*a)
    print ('x =',solution1)
else:
    solution1 = (-b-cmath.sqrt(delta))/(2*a)
    solution2 = (-b+cmath.sqrt(delta))/(2*a)

    print ('x =',solution1)
    print('x =',solution2)

What kind of class are you following?

It is just a summer class "Computational Thinking Part 1" I believe. It is a prerequisite for other computer science classes.


But I already fixed it, thanks for the help though. I forgot to add some parentheses.


* - arkoydas - Jul 3, 2017

Is there any way to do it fast with list comprehension?