teslariu

calc.py

Nov 16th, 2022
918
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.05 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. '''
  4. # bloque completo:
  5. try:
  6.    ......
  7. except .......:
  8.    ..........
  9. else:
  10.    ..........
  11. finally: # se ejecuta siempre, se usa para cerrar conexiones a based de datos, web, etc
  12.    .......
  13.  
  14.  
  15.  
  16. # valida el ingreso de un entero
  17.  
  18. while True:
  19.    try:
  20.        n = float(input("Ingrese un nro: "))
  21.    except ValueError:
  22.        print("No ha ingresado un nro")
  23.    else:
  24.        break
  25.        
  26. print(n)
  27.  
  28. def ingresar_numero():
  29.    while True:
  30.        try:
  31.            n = float(input("Ingrese un nro: "))
  32.        except ValueError:
  33.            print("No ha ingresado un nro")
  34.        else:
  35.            return n
  36.            
  37.            
  38. a = ingresar_numero()
  39. b = ingresar_numero()
  40. c = a + b
  41. print(c)
  42.  
  43. # si quiero capturar cualquier tipo de excepcion:
  44. try:
  45.    int("safdadf")
  46. except Exception:
  47.    print("Error")
  48.  
  49.  
  50. # puedo manejar varias excepciones a la vez:
  51. try:
  52.    n = int(input("Ingrese un nro: "))
  53. except (ValueError, ZeroDivisionError):
  54.    print("Error")
  55.  
  56.    
  57. try:
  58.    n = int(input("Ingrese un nro: "))
  59.    n = n /
  60. except ValueError:
  61.    print("Error de valor")
  62. except ZeroDivisionError:
  63.    print("No se puede dividir por cero")
  64.  
  65.  
  66. # Lanzar excepciones (raise):
  67. def sumar(a,b):
  68.    """Función que toma dos nros como argumentos
  69.    y devuelve su suma"""
  70.    if not isinstance(a,(float,int)) or not isinstance(b,(float,int)):
  71.        raise TypeError("Se necesitan dos nros")
  72.    return a + b
  73.    
  74. print(sumar(14,45))
  75. print(sumar("Hola","Chau"))
  76. print(sumar([1,2,3],[100,200,300]))
  77. '''
  78. # Script que implementa una calculadora bàsica
  79. # si quieren reutilizar còdigo grabar como calc.py
  80.  
  81. def ingresar_numero():
  82.     while True:
  83.         try:
  84.             n = float(input("Ingrese un nro: "))
  85.         except ValueError:
  86.             print("No ha ingresado un nro")
  87.         else:
  88.             return n
  89.            
  90. def menu():
  91.     return """
  92.    Menu de opciones
  93.    1. Suma de 2 nros
  94.    2. Resta de dos nros
  95.    3. Producto de 2 nros
  96.    4. Cociente de 2 nros
  97.    5. Salir
  98.    """
  99.  
  100. def suma():
  101.     a = ingresar_numero()
  102.     b = ingresar_numero()
  103.     return a + b
  104.  
  105. def resta():
  106.     a = ingresar_numero()
  107.     b = ingresar_numero()
  108.     return a - b
  109.  
  110. def producto():
  111.     a = ingresar_numero()
  112.     b = ingresar_numero()
  113.     return a * b
  114.    
  115. def cociente():
  116.     a = ingresar_numero()
  117.     b = ingresar_numero()
  118.     if not b:
  119.         return "No se puede dividir por cero"
  120.     return a / b
  121.    
  122.    
  123. if __name__ == '__main__':
  124.     print("Calculadora básica")
  125.     while True:
  126.         print(menu())
  127.         opcion = input("Seleccione una opción: ")
  128.         if opcion == "1":
  129.             print(suma())
  130.        
  131.         elif opcion == "2":
  132.             print(resta())
  133.    
  134.         elif opcion == "3":
  135.             print(producto())
  136.    
  137.         elif opcion == "4":
  138.             print(cociente())
  139.        
  140.         elif opcion == "5":
  141.             print("Hasta luego")
  142.             break
  143.    
  144.         else:
  145.             print("Opción incorrecta")
  146.  
Advertisement
Add Comment
Please, Sign In to add comment