Advertisement
teslariu

raiz_cuadrada

May 15th, 2021
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.04 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. """
  4. Script que calcula las posibles soluciones de una raíz cuadrada de un
  5. número real. Si no existe, imprime una cartel indicando tal situación
  6. """
  7.  
  8. def calcular_raiz(numero):
  9.     from math import sqrt
  10.     if numero < 0:
  11.         return "NO EXISTE la raiz cuadrada de un nro. negativo en el campo de los reales"
  12.     elif numero == 0:
  13.         return 0
  14.     else:
  15.         return [sqrt(numero),-1*sqrt(numero)]
  16.        
  17. def imprimir_raices(raices):
  18.     if isinstance(raices,str):
  19.         print(raices)
  20.     elif isinstance(raices, int):
  21.         print(f"Existe una única raiz: {raices}")
  22.     else:
  23.         print(f"Existen dos soluciones: {raices[0]} y {raices[1]}")
  24.        
  25.  
  26. while True:
  27.    
  28.     numero = float(input("Ingrese un nro: "))
  29.     raices = calcular_raiz(numero)
  30.     imprimir_raices(raices)
  31.    
  32.     opcion = input("Presione cualquier tecla para continuar ('X' para salir): ")
  33.     if opcion.casefold() == "x":
  34.         print("Gracias por usar este programa...")
  35.         break
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement