Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
solve equation
#1
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 ?
Reply
#2
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Solve an equation hughng92 3 4,717 Jan-26-2017, 04:35 PM
Last Post: ichabod801

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020