-1

The following code is what I'm using for a quadratic problem solver, where a cannot equal 0. I am currently getting really odd answers, and can't seem to figure out the problem. Thoughts?

def descriminant(a, b, c): #Setting input perameters for descriminant
if a>0 or a<0: #If a isnt equal to 0, find descriminant
    disc = (b**2-4*a*c) #Defining what descriminant does with disc
    if disc>0:
        disc1=float(disc)**0.5
        return disc1 #returns and allows to be used again instead of print which doesnt allow you to use it again

    else:
        print("The discriminant must be greater than 0")
else: #if a is equal to 0
    print ("A cannot equal 0") #Tell the user a cant equal 0


def quad_form(a, b, c): #Defining quad form w/ input a, b, c

disc2=float(descriminant(a, b, c))


quad_form1=((-1*b) + disc2/float((2*a))) #Defining + forumula for quad form
quad_form2=((-1*b) - disc2/float((2*a))) #Defining - forumula for quad form

return quad_form1, quad_form2


UI=input("Enter the coefficients of a quadratic separated by commas where A is not equal to zero: ") #User Input
a=float(UI[0])
b=float(UI[1])
c=float(UI[2])

print quad_form(a, b, c)
Addison
  • 403
  • 8
  • 24
  • 1
    What are the expected answers and what are the actual answers? Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: [How to create a Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve). – MikeJRamsey56 Dec 05 '16 at 05:02

1 Answers1

1

It looks like you're taking square root twice. Once in discriminant() and then in quad_form(). You should remove one of them.

EDIT: Your quad_form expressions are also wrong:

quad_form1=((-1*b) + disc3/float((2*a)))

should be

quad_form1=((-1*b) + disc3)/float((2*a))

or more readably

quad_form1=(-b + disc3)/float(2*a).

Similarly for the next line,

quad_form1=((-1*b) - disc3)/float((2*a))
Aditya Kashi
  • 266
  • 2
  • 13
  • This better @Aditya Kashi ? I don't get the right answer still – Addison Dec 05 '16 at 00:25
  • Just directly use disc2 in the formula. Don't cast it to `int`. Also, are the user's inputs necessarily integers? In either case, you should make a,b and c `float` instead of `int`. – Aditya Kashi Dec 05 '16 at 00:29
  • Ok I made those Changes, but it doesn't seem to help with the output. The numbers just aren't correct, i have used online calculators and done it myself @Aditya Kashi – Addison Dec 05 '16 at 00:33
  • Check the placements of brackets in your formulae. See the edit. – Aditya Kashi Dec 05 '16 at 00:40