Advertisement
teslariu

excepciones 1

Aug 10th, 2022
1,027
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.15 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. #
  4. # Excepciones: es un modelo de manejo de errores
  5. '''
  6. n = 5
  7. try:
  8.    total = 23/n
  9. except ZeroDivisionError:
  10.    print("No se puede dividir por cero")
  11. else:
  12.    print(total)
  13.    
  14. # Script que pide un numero y devuelve su cuadrado
  15. while True:
  16.    try:
  17.        numero = float(input("Ingrese un nro: "))
  18.    except ValueError:
  19.        print("No ha ingresado un numero")
  20.    else:
  21.        break
  22. print(f"El cuadrado de {numero} es {numero**2}")
  23. '''
  24. # Script que pide el ingreso de numeros naturales y devuelve su promedio.
  25. # La carga de nros termina al ingresar un nro negativo o cero
  26.  
  27. def ingresar_entero():
  28.     while True:
  29.         try:
  30.             n = int(input("Ingrese un nro: "))
  31.         except ValueError:
  32.             print("No ha ingresado un nro entero")
  33.         else:
  34.             return n
  35.        
  36.  
  37.  
  38. numeros = []
  39. while True:
  40.     numero = ingresar_entero()
  41.     if numero > 0:
  42.         numeros.append(numero)
  43.     else:
  44.         break
  45.  
  46. if numeros:
  47.     print(f"Numeros: {numeros}")
  48.     print(f"Promedio: {sum(numeros)/len(numeros)}")
  49. else:
  50.     print("La lista de numeros esta vacia")
  51.    
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement