-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path05 Quadratic function.py
More file actions
43 lines (34 loc) · 999 Bytes
/
05 Quadratic function.py
File metadata and controls
43 lines (34 loc) · 999 Bytes
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
#!/usr/bin/env python2.6
#_*_coding: utf8_*_
from math import *
from math import sqrt
def __main__():
print '''
Ecuaciones de 2do. Grado
ax^2 + bx + c = 0
'''
a = input('Coeficiente de A: ')
b = input('Coeficiente de B: ')
c = input('Coeficiente de C: ')
try:
# Si el discriminante es menor que cero, el resultado es imaginario.
if ( b ** 2 - 4*a*c ) < 0:
r = -b / (2.0*a)
i = (sqrt(-(b ** 2 - 4*a*c)))/(2*a)
print '''
Solución:
X (+): %f + %f
X (-): %f - %f
''' % (r,i,r,i)
# En caso contrario, el resultado es real.
else:
(X1,X2) = (-b + sqrt(b ** 2 - 4*a*c))/(2*a),(-b - sqrt(b ** 2 - 4*a*c))/(2*a)
print '''
Solución:
X (+) %f
X (-) %f
''' % (X1,X2)
except:
print '\n Ha ocurrido un error'
if __name__ == "__main__":
__main__()