teslariu

cuadr con excep

Nov 13th, 2021
686
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.35 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. #
  4. """
  5. Script que calcula las raíces de una ecuación cuadrática en el campo
  6. de los nros reales
  7. Ej:
  8. >>> Ingrese el término cuadrático a: 1
  9. >>> Ingrese el término lineal b: 0
  10. >>> Ingrese el término independiente c: 36
  11. >>> No existen raíces
  12. """
  13.  
  14. from math import sqrt
  15. from datetime import datetime
  16.  
  17.  
  18. def ingresar_numero(n):
  19.     while True:
  20.         try:
  21.             numero = float(input(f"Ingrese el numero {n}: "))
  22.         except ValueError:
  23.             print("Debe ingresar un número")
  24.         else:
  25.             if n == "a" and not numero:
  26.                 print("a debe ser diferente de cero")
  27.             else:
  28.                 return numero
  29.  
  30.  
  31. def calcular_raices(a,b,c):
  32.     delta = b**2 - 4*a*c
  33.    
  34.     if delta > 0:
  35.         raiz1 = (-1*b + sqrt(delta)) / (2*a)
  36.         raiz2 = (-1*b - sqrt(delta)) / (2*a)
  37.         return [raiz1, raiz2]
  38.    
  39.     elif delta < 0:
  40.         return "No existen soluciones en el campo real"
  41.        
  42.     else:
  43.         raiz = -1*b / (2*a)
  44.         return raiz
  45.  
  46.    
  47. def imprimir(raices):
  48.     if isinstance(raices, float):
  49.         print("Existe una raíz: {:.2f}".format(raices))
  50.        
  51.     elif isinstance(raices, list):
  52.         print("Existen dos raíces: {:.2f} y {:.2f}".format(raices[0], raices[1]))
  53.    
  54.     else:
  55.         print(raices)
  56.  
  57.    
  58. def grabar_datos(a,b,c,raices):
  59.     # Abro el archivo cuadratica.txt en modo append
  60.     # si no existe lo crea
  61.     f = open("cuadratica.txt", "a")
  62.     # escribo fecha y hora del cálculo:
  63.     f.write(datetime.now().strftime('%d/%m/%y %H:%M:%S'))
  64.     f.write("\nParámetros de la parábola:\n")
  65.     f.write(f"Término cuadrático (a): {a}\n")
  66.     f.write(f"Término lineal (b): {b}\n")
  67.     f.write(f"Término independiente (c): {c}\n")
  68.    
  69.     if isinstance(raices, float):
  70.         f.write("Existe una raíz: {:.2f}\n".format(raices))
  71.        
  72.     elif isinstance(raices, list):
  73.         f.write("Existen dos raíces: {:.2f} y {:.2f}\n".format(raices[0], raices[1]))
  74.    
  75.     else:
  76.         f.write(raices + "\n")
  77.     f.write("------------------------------------------------\n")
  78.     f.close()
  79.    
  80.    
  81.  
  82.  
  83. #################################################
  84.  
  85. while True:
  86.     print("\nCálculo de raíces de una ecuación cuadrática")
  87.     a = ingresar_numero("a")
  88.     b = ingresar_numero("b")
  89.     c = ingresar_numero("c")
  90.     raices = calcular_raices(a,b,c)
  91.     imprimir(raices)
  92.     grabar_datos(a,b,c,raices)
  93.    
  94.     opcion = input("\nPresione cualquier tecla para continuar ('1' para salir): ")
  95.     if opcion == "1":
  96.         print("Gracias por usar este programa....")
  97.         break
  98.  
Advertisement
Add Comment
Please, Sign In to add comment