Advertisement
teslariu

ej while/if

Apr 27th, 2023
823
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.87 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. #
  4.  
  5. """
  6. Escribir un programa que le pida a un usuario que ingrese una sucesión
  7. de nros naturales. La carga debe terminar cuando se ingrese -1, y se
  8. debe mostrar la cantidad de nros ingresados, la suma total de los valores y su promedio
  9. """
  10. # Estrategias de resolución
  11. # 1) Determinar que lógica usar (condicional, bucle)
  12. # 2) Determinar como vamos a guardar los valores (variables)
  13. numeros = []
  14.  
  15. while True:
  16.     numero = int(input("Ingrese un nro entero no negativo (-1 para salir): "))
  17.     if numero >= 0:
  18.         numeros.append(numero)
  19.        
  20.     elif numero == -1:
  21.         break
  22.        
  23.     else:
  24.         print("Error: número negativo")
  25. print(f"Cantidad de nros ingresados: {len(numeros)}")
  26. print(f"Suma de nros ingresados: {sum(numeros)}")
  27. print(f"Promedio de nros ingresados: {sum(numeros)/len(numeros):.2f}")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement