Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

Python 

Help making quadratic formula in python

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
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)
Public Honeypot Project: https://github.com/Slober3/PotHead
Honeypot Twitter: https://twitter.com/HoneyHive9
BTC: 1Anime1QrjwsEmz8bC2FwVcNXDLxP3SWAJ

Erin Wrote:It’s fine. There’s nothing to be afraid of. Calm down, Lilan
(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.
(Jul 2, 2017 01:41 PM)Gerymc 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?
Public Honeypot Project: https://github.com/Slober3/PotHead
Honeypot Twitter: https://twitter.com/HoneyHive9
BTC: 1Anime1QrjwsEmz8bC2FwVcNXDLxP3SWAJ

Erin Wrote:It’s fine. There’s nothing to be afraid of. Calm down, Lilan
(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.
Is there any way to do it fast with list comprehension?


Possibly Related Threads...
Thread / Author Replies Views Last Post
[Python] by Bass_ReevesAug 20, 2023 01:44 AM 9 632 Aug 20, 2023 01:44 AM
Last Post: Lucky4L
[Python] by The PactJul 13, 2017 04:35 PM 3 150 Jul 13, 2017 04:35 PM
Last Post: Goodies
[Shell] by jahwiAug 20, 2016 03:15 PM 3 195 Aug 20, 2016 03:15 PM
Last Post: gogglebot