0

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))
Pang
  • 9,564
  • 146
  • 81
  • 122

2 Answers2

1
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 (**)

0
import math as m

print('QUADRATIC EQUATION FORM - (a)x^2 + (b)x + (c) = 0')
a = float(input('Enter the value of a '))
b = float(input('Enter the value of b '))
c = float(input('Enter the value of c '))

check = b**2 - 4*a*c

##### FOR NO DEFINITE ROOTS #####
if check < 0 :
  print("No real root")

##### FOR TWO DISTINCT ROOTS #####

if check > 0 :
  print("Two Distinct roots - ")
  root_1 = -b + m.sqrt(b**2 - 4*a*c)
  root_2 = -b - m.sqrt(b**2 - 4*a*c)
  print('Root 1 = ',root_1)
  print('Root 2 = ',root_2)

##### FOR EQUAL ROOTS #####

if check == 0 :
  print("Two Equal roots - ")
  root_1 = -b + m.sqrt(b**2 - 4*a*c)
  root_2 = root_1
  print('Root 1 = ',root_1)
  print('Root 2 = ',root_2)
Mukunth S
  • 1
  • 4
  • [A code-only answer is not high quality](//meta.stackoverflow.com/questions/392712/explaining-entirely-code-based-answers). While this code may be useful, you can improve it by saying why it works, how it works, when it should be used, and what its limitations are. Please [edit] your answer to include explanation and link to relevant documentation. – Muhammad Mohsin Khan Apr 06 '22 at 12:03