-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquadraatic_ans_pavel.py
More file actions
64 lines (55 loc) · 1.97 KB
/
quadraatic_ans_pavel.py
File metadata and controls
64 lines (55 loc) · 1.97 KB
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#!/usr/bin/env python3
# Copyright (c) 2008-11 Qtrac Ltd. All rights reserved.
# This program or module is free software: you can redistribute it and/or
# modify it under the terms of the GNU General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version. It is provided for educational
# purposes and is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
import cmath
import math
import sys
def get_float(msg, allow_zero):
x = None
while x is None:
try:
x = float(input(msg))
if not allow_zero and abs(x) < sys.float_info.epsilon:
print("zero is not allowed")
x = None
except ValueError as err:
print(err)
return x
print("ax\N{SUPERSCRIPT TWO} + bx + c = 0")
a = get_float("enter a: ", False)
b = get_float("enter b: ", True)
c = get_float("enter c: ", True)
x1 = None
x2 = None
discriminant = (b**2) - (4 * a * c)
if discriminant == 0:
x1 = -(b / (2 * a))
else:
if discriminant > 0:
root = math.sqrt(discriminant)
else: # discriminant < 0
root = cmath.sqrt(discriminant)
x1 = (-b + root) / (2 * a)
x2 = (-b - root) / (2 * a)
if b == 0:
equation = ("{0}x\N{SUPERSCRIPT TWO} + {2} = 0"
" \N{RIGHTWARDS ARROW} x = {3}").format(a, b, c, x1)
elif c == 0:
equation = ("{0}x\N{SUPERSCRIPT TWO} + {1}x = 0"
" \N{RIGHTWARDS ARROW} x = {3}").format(a, b, c, x1)
elif b==0 and c == 0:
equation = ("{0}x\N{SUPERSCRIPT TWO} = 0"
" \N{RIGHTWARDS ARROW} x = {3}").format(a, b, c, x1)
else:
equation = ("{0}x\N{SUPERSCRIPT TWO} + {1}x + {2} = 0"
" \N{RIGHTWARDS ARROW} x = {3}").format(a, b, c, x1)
if x2 is not None:
equation += " or x = {0}".format(x2)
print(equation)