Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (16.4k points)

My program doesn't appear to give me the correct solutions. Now and again it does, at times it doesn't. I can't discover my error. Any Suggestions?

import math

a,b,c = input("Enter the coefficients of a, b and c separated by commas: ")

d = b**2-4*a*c # discriminant

if d < 0:

    print "This equation has no real solution"

elif d == 0:

    x = (-b+math.sqrt(b**2-4*a*c))/2*a

    print "This equation has one solutions: ", x

else:

    x1 = (-b+math.sqrt(b**2-4*a*c))/2*a

    x2 = (-b-math.sqrt(b**2-4*a*c))/2*a

    print "This equation has two solutions: ", x1, " and", x2

1 Answer

0 votes
by (26.4k points)

This line is responsible for probelm

(-b+math.sqrt(b**2-4*a*c))/2*a

You actually need to add more parentheses. Just change x/2*a into (x/2)*2

(-b + math.sqrt(b**2 - 4*a*c)) / (2 * a)

Additionally, in case you're as of now storing d, why not use it?

x = (-b + math.sqrt(d)) / (2 * a)

Improve your knowledge in Python from scratch using python online courses

To know more about this you can have a look at the following video tutorial:-

Related questions

0 votes
4 answers
asked Apr 4, 2021 in Python by laddulakshana (16.4k points)
0 votes
4 answers
0 votes
4 answers

Browse Categories

...