Advertisement
teslariu

aritmetica

Aug 31st, 2022
676
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.14 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3.  
  4. """
  5. Módulo que implementa funciones aritméticas
  6. """
  7. __author__ = "YO"
  8. __copyright__ = "Curso de Python"
  9. __credits__ = ["Ana","Hugo"]
  10. __license__ = "GPL"
  11. __version__ = "1.0"
  12. __email__ = "alsad@gmail.com"
  13. __status__ = "Development"
  14.  
  15.  
  16.  
  17. def ingresar_numero(n=""):
  18.     """Función que valida el ingreso de un nro"""
  19.     while True:
  20.         try:
  21.             numero = float(input(f"Ingresar {n}: "))
  22.         except ValueError:
  23.             print("Debe ingresar un nro...")
  24.         else:
  25.             return numero
  26.  
  27. def titulo():
  28.     """Función que retorna un tìtulo de un script"""
  29.     return """
  30.    Calculadora básica
  31.    ==================
  32.    """
  33.  
  34.  
  35. def menu():
  36.     return """
  37.    Menu de opciones
  38.    ----------------
  39.    1. Suma
  40.    2. Resta
  41.    3. Multiplicación
  42.    4. División
  43.    5. Salir
  44.    """
  45.  
  46. def suma(a,b):
  47.     return f"{a} + {b}: {a + b}"
  48.    
  49. def resta(a,b):
  50.     """Función que toma como parámetros dos nros y devuelve su suma"""
  51.     return f"{a} - {b}: {a - b}"
  52.  
  53.    
  54. def multiplica(a,b):
  55.     return f"{a} * {b}: {a * b}"
  56.  
  57.    
  58. def divide(a,b):
  59.     if not b:
  60.         return "No se puede dividir por cero"
  61.     else:
  62.         return f"{a} / {b}: {a / b:.2f}"
  63.    
  64.  
  65. if __name__ == '__main__':
  66.     print(titulo())
  67.  
  68.     while True:
  69.         print(menu())
  70.  
  71.         opcion = input("Seleccione una opción: ")
  72.  
  73.         if opcion == "1":
  74.             a = ingresar_numero("a")
  75.             b = ingresar_numero("b")
  76.             print(suma(a,b))
  77.    
  78.         elif opcion == "2":
  79.             a = ingresar_numero("a")
  80.             b = ingresar_numero("b")
  81.             print(resta(a,b))
  82.    
  83.         elif opcion == "3":
  84.             a = ingresar_numero("a")
  85.             b = ingresar_numero("b")
  86.             print(multiplica(a,b))
  87.    
  88.         elif opcion == "4":
  89.             a = ingresar_numero("a")
  90.             b = ingresar_numero("b")
  91.             print(divide(a,b))
  92.    
  93.         elif opcion == "5":
  94.             print("Gracias por usar este programa...")
  95.             break
  96.    
  97.         else:
  98.             print("Opción incorrecta...")
  99.    
  100.    
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement