Advertisement
teslariu

cuadratica_error

Sep 11th, 2021
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.98 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. #
  4. """
  5. Script que calcula las reices de una ec. cuadratica en el campo de los
  6. nros reales y lo guarda en un archivo "raices.txt "previamente creado
  7. """
  8.  
  9. import datetime    # para agregar fecha y hora
  10.  
  11. def validar_numeros(p):
  12.     while True:
  13.         try:   
  14.             numero = float(input(f"Ingrese el término {p}: "))
  15.         except ValueError:
  16.             print("Error, debe ingresar un nro")
  17.         else:
  18.             if p == "a" and not numero:
  19.                 print("Error, 'a' debe ser no nulo")
  20.             else:
  21.                 return numero
  22.  
  23.  
  24. def ingresar_datos():
  25.     a = validar_numeros("a")
  26.     b = validar_numeros("b")
  27.     c = validar_numeros("c")
  28.     return [a,b,c]
  29.  
  30.  
  31. def calcular_raices(a,b,c):
  32.     from math import sqrt
  33.     delta = b**2 - 4*a*c
  34.    
  35.     if not delta:
  36.         raiz = -1*b / (2*a)
  37.         return raiz
  38.    
  39.     elif delta > 0:
  40.         raiz1 = (-1*b + sqrt(delta)) / (2*a)
  41.         raiz2 = (-1*b - sqrt(delta)) / (2*a)
  42.         return [raiz1, raiz2]
  43.        
  44.     else:
  45.         return "No existen raices en el campo de los nros. reales\n"
  46.        
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement