Код: Выделить всё
import math
import cmath
print('Follow this format: Ax^2 + Bx + C')
a = float(input('Enter A: '))
b = float(input('Enter B: '))
c = float(input('Enter C: '))
#Functions
def qfpositive(a, b, c):
return(-abs(b)+cmath.sqrt(b**2-4*a*c))/2*a
def qfnegative(a, b, c):
return(-abs(b)-cmath.sqrt(b**2-4*a*c))/2*a
def format_solution(root):
if root.real == 0:
return f'(x {root.real})'
# Root is a real number
if root.real > 0:
return f'(x + {root.real})'
elif root.real < 0:
return f'(x {root.real})'
elif root.imag > 0:
# Root has a positive imaginary part
return f'(x {root.real} + {root.imag}i)'
else:
# Root has a negative imaginary part
return f'(x {root.real} - {abs(root.imag)}i)
x1 = qfpositive(a, b, c)
x2 = qfnegative(a, b, c)
print(format_solution(x1))
print(format_solution(x2))
Код: Выделить всё
if root.real > 0:
return f'(x + {root.real})'
elif root.real < 0:
return f'(x {root.real})'
Подробнее здесь: https://stackoverflow.com/questions/787 ... ange-the-f
Мобильная версия