View Single Post
Old Oct 30th, 2007, 10:42 PM   #12
acwbrat
Newbie
 
Join Date: Oct 2007
Posts: 17
Rep Power: 0 acwbrat is on a distinguished road
Re: Quadratic Formula in Python

What am I doing wrong?

import math

def main(): # beginning of the code
    print "Hello! This program finds the real and complex solutions to a quadratic"
    print

    a, b, c = input("Please enter the coefficients (a, b, c): ")

    disc = (b*b-4*a*c) # discriminant
    if disc < 0:
        disc = -disc    # force it to be positive
    else:
        print "This quadratic does not have imaginary roots"
        return

    discRoot = math.sqrt(b*b-4*a*c)

    root1r = (-b) / (2 * a)      # the real portion of root1
    root1i = discRoot / (2 * a)  # the imaginary portion of root1
    
    root2r = root1r      # the real portions are the same for each root
    root2i = -root1i     # the only difference is it's negative

main()
acwbrat is offline   Reply With Quote