teslariu

listas while

Sep 21st, 2021
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.88 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. #
  4. # Dada una lista de numeros naturales de longitud arbitraria:
  5. # a) Ingresar datos en la lista hasta ingresar -1
  6. # b) Calcular su promedio
  7. # c) Hallar el mayor valor
  8. # d) hallar el menor valor
  9.  
  10. numeros = []
  11.  
  12. while True:
  13.     numero = int(input("Ingrese un nro natural (-1 para terminar): "))
  14.     if numero != -1:
  15.         numeros.append(numero)
  16.     else:
  17.         break
  18.  
  19. # recorro con un while la lista de numeros y calculo promedio, maximo
  20. # y minimo
  21. i = 0
  22. suma = 0
  23. maximo = numeros[0]
  24. minimo = numeros[0]
  25.  
  26. while i < len(numeros):
  27.     suma = suma + numeros[i]
  28.     if numeros[i] < minimo:
  29.         minimo = numeros[i]
  30.     if numeros[i] > maximo:
  31.         maximo = numeros[i]
  32.     i = i + 1
  33.  
  34. promedio = suma / len(numeros)
  35.    
  36.  
  37. # imprimo los resultados
  38. print(f"El promedio es {promedio}")
  39. print(f"El mayor valor es {maximo}")
  40. print(f"El menor valor es {minimo}")
Advertisement
Add Comment
Please, Sign In to add comment