2
I’m solving a Python exercise that asks me to sort a list of 5 values typed by the user in ascending order, without using the Sort() method. The problem is my code isn’t working as it should. The error that is happening is that instead of printing all the ordered values, it only prints a few. For example, if I type 7, 4, 2, 8, 3 it prints [2, 4, 7, 8, 3]. I wonder where I went wrong. Follow my code:
listaNumeros = []
for i in range(0, 5):
numero = int(input('Digite um número: '))
if i == 0:
listaNumeros.append(numero)
else:
for j in range(0, len(listaNumeros)):
if numero < listaNumeros[j]:
listaNumeros.insert(j, numero)
break
else:
if numero > listaNumeros[j]:
listaNumeros.append(numero)
break
print(listaNumeros)
See if this post helping.
– Paulo Marques
Sorting algorithms
– Augusto Vasques