forked from sofeien/Python3-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path6.py
45 lines (39 loc) · 999 Bytes
/
6.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
41
42
43
44
45
import sys
import cmath
import math
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:
root=cmath.sqrt(discriminant)
x1=(-b+root)/(2*a)
x2=(-b-root)/(2*a)
def to_int(a):
if a.is_integer():
return int(a)
return a
equation=("{0}x\N{SUPERSCRIPT TWO}+{1}x+{2}=0"
"\N{RIGHTWARDS ARROW} x={3:.2f}".format(to_int(a),to_int(b),to_int(c),x1))
if x2 is not None:
equation += " or x={:.2f}".format(x2)
print(equation)