It is required to solve a quadratic equation with the help of the Viet theorem. That's what happened with me, but it does not perform the task correctly.

print("Значения ax^2+bx+c=0") a = float(input("a = ")) b = float(input("b = ")) c = float(input("c = ")) def vieto(z,v,x): if a ** 2 != 0: x1 = -b/a x2 = c/a print(x1,x2) else: print("Нет корней.") f = vieto(a,b,c) print(f) 
  • What numbers do you enter, what get and what you want to get? - Igor
  • and what, in your opinion, is the Vietta theorem? - Igor
  • I enter such numbers: a = 2, b = 8, c = 2. I want to receive: -0.27, -3.73. - Sofanchik
  • Does not perform the task correctly because the calculations in the code are not even remotely related to the Vietta theorem. I think you need to re-read it. Ps. Although not a bit similar. - Enikeyschik
  • @Sofanchik 1. Your roots are rounded, because 'x1 * x2 = 1.0071', and not 1 exactly. 2. The function accepts arguments z, v, x, but never uses them anywhere, but uses non-existing local a, b, and c. - Pavel_K

2 answers 2

 def v(a, b, c): x1 = x2 = 0 points = [i for i in range(-100, 100)] for i in points: x1 = i for j in points: x2 = j if x1 + x2 == -b / a and x1 * x2 == c / a: return x1, x2 if __name__ == '__main__': v(#Тут ваши аргументы) 

Will work only for integers.
You can also change the range in order to increase the search area

     def drange(x, y, jump) -> float: # Генератор чисел float import decimal while x < y: yield float(x) x += decimal.Decimal(jump) def viet(a, b, c): # Подбор корней по теореме Виетта D = (b**2 - 4*a*c)**0.5 x1 = x2 = 0 points = [i for i in drange(-5, 5, '0.001')] if D > 0: for i in points: x1 = i for j in points: x2 = j if round(x1 + x2, 2) == round(-b / a, 2) and round(x1 * x2, 2) == round(c / a, 2): return x1, x2 if D == 0: for i in points: x1 = i if round(2 * x1, 2) == round(-b / a, 2) and round(x1 ** 2, 2) == round(c / a, 2): return x1 if D < 0: print('Корней нет!') a, b, c = float(input('a = ')), float(input('b = ')), float(input('c = ')) print(viet(a, b, c)) 

    The list of root points here is from -5 to 5 with an accuracy of up to 0.001, i.e. 10,000 options. Increasing the range, increases the time of the search.