pythonmathformulaquadratic

Need Help for Quadratic formula on python


I just started learning Python in school, here is my code for the quadratic formula solver. Problem is on line 4.

a=int(input('a= ')) # A-stvis mnishvnelobis micema
b=int(input('b= ')) # B-stvis mnishvnelobis micema
c=int(input('c= ')) # C-stvis mnishvenlobis micema
int(a)*(x2)+int(b)*x+c=0
d=(-b2)-4*a*c
x1=-b+(d**(1/2))
x2=-b-(d**(1/2))

Solution

  • from math import sqrt
    
    a = int(input('a= ')) # A-stvis mnishvnelobis micema
    b = int(input('b= ')) # B-stvis mnishvnelobis micema
    c = int(input('c= ')) # C-stvis mnishvenlobis micema
    d = b**2 - 4*a*c
    x1 = (-b - sqrt(d))/2
    x2 = (-b + sqrt(d))/2
    
    print("x1 =", x1)
    print("x2 =", x2)
    

    Your equation is not needed, and python doesn't understand it. You can comment it if you want.

    try and use the square root (sqrt) instead of exponentiation (**)