Python Forum

Full Version: solve equation
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
hi, i did a python program to solve a 2nd degree equation:

from math import *


a = input('a = ')
b = input('b = ')
c = input('c = ')
print 'The equation is : {}x^2 + {}x + {} = 0 '.format(a, b, c)
delta = b*b-4*a*c

if delta > 0:
    print 'Discriminant = {}, two solutions:'.format(delta)
    r=sqrt(delta)
    t = -b - r
    t2 = -b + r
    s1 = t / (2*a)
    s2 = t2 / (2*a)
    print s1
    print s2
elif delta == 0:
    print 'Discriminant = 0 : one solution : '
    s = -b/(2*a)
    print s
else:
    print "Discriminant = {} : no solutions".format(delta)
so the output if a=4, b= 8, c=8 (delta=0) would be :
Output:
The equation is : 4x^2 + 8x + 4 = 0 Discriminant = 0 : one solution : -1
or if a=4, b=8, c=14 (delta<0):
Output:
The equation is : 4x^2 + 8x + 14 = 0 Discriminant = -160 : no solutions
or if a=4, b=16, c=8 (delta>0):
Output:
The equation is : 4x^2 + 16x + 8 = 0 Discriminant = 128, two solutions: -3.41421356237 -0.585786437627
But what i would like to do is to write the whole equation in only one input like:
equation = input('write the equation : ')
#Write the equation : 4x^2 + 4x + 7 = 0
and then find the discriminant of the equation written in the input.

Another way would maybe be to write the equation as argument ?
and so i would run my python like:
python mypython.py "equation"
what would be the easiest way to do it ?
write is as a function with parameters a, b, and c passed as arguments:
def 2nd_poly(a, b, c):
Heres a challange, write it so it will handle any degree poly.