teslariu

raice

Jun 15th, 2021
209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.41 KB | None | 0 0
  1. /usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. #
  4. """
  5. Programa que calcula las raices de una ecuación cuadrática
  6. y = ax**2 + bx + c
  7. en el campo de los números reales
  8. """
  9. from math import sqrt
  10.  
  11. def cargar_datos():
  12.     a = float(input("Ingrese el coeficiente cuadrático 'a': "))
  13.     b = float(input("Ingrese el coeficiente lineal 'b': "))
  14.     c = float(input("Ingrese el coeficiente independiente 'c': "))
  15.     return [a,b,c]
  16.    
  17.  
  18.  
  19. def calcular_raices(a,b,c):
  20.     delta = b**2 - 4*a*c
  21.    
  22.     if not delta:
  23.         return -b / 2 * a
  24.     elif delta > 0:
  25.         raiz1 = (-b + sqrt(delta)) / (2*a)
  26.         raiz2 = (-b - sqrt(delta)) / (2*a)
  27.         return [raiz1, raiz2]
  28.     else:
  29.         return None
  30.        
  31.        
  32. def imprimir(raices):
  33.     if raices is None:
  34.         print("No existe solución en el campo de los nros. reales")
  35.     elif isinstance(raices,float):
  36.         print(f"Existe una sola raiz: {raices}")
  37.     else:
  38.         print(f"Existen dos raices. Raíz 1: {raices[0]} - Raiz 2: {raices[1]}")
  39.        
  40.  
  41.        
  42.        
  43.        
  44.  
  45.  
  46. print("Programa que calcula las raices de una ecuacion cuadrática")
  47. while True:
  48.    
  49.     a,b,c = cargar_datos()
  50.     raices = calcular_raices(a,b,c)
  51.     imprimir(raices)
  52.    
  53.     opcion = input("Presione cualquier tecla para continuar ('1' para salir): ")
  54.     if opcion == "1":
  55.         print("Gracias por usar este programa ...")
  56.         break
Advertisement
Add Comment
Please, Sign In to add comment