Advertisement
teslariu

ej funciones

Aug 30th, 2022
804
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.58 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. #
  4. # Script que implementa una calculadora simple de nros naturales
  5.  
  6. def menu():
  7.     return """
  8.    Menu de opciones
  9.    ----------------
  10.    1) sumar
  11.    2) restar
  12.    3) multiplicar
  13.    4) dividir
  14.    5) Salir
  15.    """
  16.  
  17.  
  18. def ingresar(num):
  19.     while True:
  20.         n = input(f"Ingrese un nro {num}: ")
  21.         if n.isdecimal():
  22.             return int(n)
  23.         else:
  24.             print("Error: ingrese un entero mayor o igual a cero")
  25.  
  26. def suma(a,b):
  27.     return f"a + b = {a+b}"
  28.  
  29. def resta(a,b):
  30.     return f"a - b = {a-b}"
  31.    
  32. def division(a,b):
  33.     return f"a / b = {a/b}"
  34.    
  35. def multiplicacion(a,b):
  36.     return f"a * b = {a*b}"
  37.  
  38.  
  39.  
  40.  
  41.  
  42. print("Calculadora simple de numeros naturales")
  43.  
  44. while True:
  45.     print(menu())
  46.     opcion = input("Seleccione una opción: ")
  47.    
  48.     if opcion == "1":
  49.         a = ingresar("a")
  50.         b = ingresar("b")
  51.         print(suma(a,b))
  52.    
  53.    
  54.     elif opcion == "2":
  55.         a = ingresar("a")
  56.         b = ingresar("b")
  57.         print(resta(a,b))
  58.    
  59.    
  60.     elif opcion == "3":
  61.         a = ingresar("a")
  62.         b = ingresar("b")
  63.         print(multiplicacion(a,b))
  64.    
  65.    
  66.     elif opcion == "4":
  67.         a = ingresar("a")
  68.         while True:
  69.             b = ingresar("b")
  70.             if not b:
  71.                 print("No se puede dividir por cero")
  72.             else:
  73.                 break
  74.         print(division(a,b))
  75.    
  76.    
  77.     elif opcion == "5":
  78.         print("Hasta luego...")
  79.         break
  80.     else:
  81.         print("Opción incorrecta")
  82.    
  83.  
  84.  
  85.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement