-
Notifications
You must be signed in to change notification settings - Fork 0
/
Completeing the square.py
40 lines (34 loc) · 1.14 KB
/
Completeing the square.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import operator
def main():
print('''Please enter the quadratic by giving these values:
ax^2+bx+c=y.''')
a = float(input('Value of a: '))
b = float(input('Value of b: '))
c = float(input('Value of c: '))
if a < 0:
print('There is no square root of negative numbers')
raise ZeroDivisionError
root_of_a = operator.pow(a, 0.5)
equa_part_1 = operator.mul(b, operator.truediv(1 / 2, root_of_a))
ans_calc_part_2 = operator.pow(equa_part_1, 2)
ans_part_1 = ans_calc_part_1(a, root_of_a, ans_calc_part_2)
new_part_1 = operator.mul(equa_part_1, -1)
ans_part_2 = operator.add(new_part_1, c)
if ans_part_2 > 0:
ans_part_2 = f'+ {ans_part_2}'
ans = f'''The complete square of that quadratic equation is:
{ans_part_1}^2 {ans_part_2}'''
print(ans)
def ans_calc_part_1(a, part_1, part_2):
if a <= 0:
return 0
if part_2 >= 0:
is_negative = f'+ {part_2}'
elif part_2 < 0:
is_negative = f'- {part_2}'
else:
return 1
if part_1.is_integer():
return f'({round(part_1)}x {is_negative})'
return f'(({part_1})x {is_negative})'
main()