Advertisement
teslariu

while3

Dec 2nd, 2021
1,057
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.73 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. #
  4. # Script que pide nros enteros positivos. La carga de nros debe concluir
  5. # al ingresar un cero, y debe mostrar entonces la suma de todos los numeros
  6. # ingresados y su promedio
  7.  
  8. # opcion 1 (recomendada)
  9. lista = []
  10.  
  11. while True:
  12.     numero = int(input("Ingrese un nro natural (o -1 para terminar): "))
  13.     if numero == -1:
  14.         break
  15.     lista.append(numero)
  16.  
  17. print(f"Suma: {sum(lista)}")
  18. print(f"Promedio: {sum(lista)/len(lista)}")
  19.  
  20. # opcion 2
  21. lista = []
  22. suma = 0
  23.  
  24. while True:
  25.     numero = int(input("Ingrese un nro natural (o -1 para terminar): "))
  26.     if numero == -1:
  27.         break
  28.     lista.append(numero)
  29.     suma = suma + numero
  30.  
  31. print(f"Suma: {suma}")
  32. print(f"Promedio: {suma/len(lista)}")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement