teslariu

func excep

Feb 13th, 2021
205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.18 KB | None | 0 0
  1. !/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. #
  4. # Crear una minicalculadora que pida 2 nros a y b y devuelva las 4 op
  5. # aritmeticas fundamentales (+,-,/,*). Tener en cuenta todos los
  6. # posibles errores
  7.  
  8. def ingresar_numero(nro):
  9.     while True:
  10.         try:
  11.             numero = float(input(f"Ingrese un nro {nro}: "))
  12.         except ValueError:
  13.             print(f"Error, debe ingresar un nro {nro}")
  14.         else:
  15.             return numero
  16.        
  17.            
  18. def suma(a,b):
  19.     return a + b
  20.    
  21. def resta(a,b):
  22.     return a - b
  23.    
  24. def producto(a,b):
  25.     return a * b
  26.  
  27. def cociente(a,b):
  28.     if b:
  29.         return a / b
  30.     else:
  31.         raise ZeroDivisionError
  32.  
  33.  
  34. while True:
  35.     a = ingresar_numero("a")
  36.     b = ingresar_numero("b")
  37.    
  38.     print(f"{a} + {b} = {suma(a,b)}")
  39.     print(f"{a} - {b} = {resta(a,b)}")
  40.     try:
  41.         print(f"{a} / {b} = {cociente(a,b)}")
  42.     except ZeroDivisionError:
  43.         print(f"{a} / {b}: No se puede dividir por cero")
  44.    
  45.     print(f"{a} * {b} = {producto(a,b)}")
  46.    
  47.        
  48.     opcion = input("Presione cualquier tecla ('X' para salir): ")
  49.     if opcion.casefold() == "x":
  50.         print("Adios..")
  51.         break
Advertisement
Add Comment
Please, Sign In to add comment