Let's see ...
You need:
- receive values to sort into a list
- such ordering needs to be done as you receive such values
- You can not use the
.sort(...)
Useful information to solve the problem
You should use features that deal with positioning a data in the list at the time of insertion. In this case, the .insert(index, valor)
method.
This method, when inserted in the given position, pushes to the right the value that previously was there. Example:
a = [1,2,3,4,5]
a.insert(2, 'oi!')
When you enter 'oi'
in position 2 of the a
list, we get the following setting for a
:
[1,2,'oi',3,4,5]
To effectively create the list in an orderly fashion without the .sort(...)
method, you will also need to loop nested (loop ). You may also need a tag variable - a flag , as we call it in the area.
Example of how the code could look:
lista = []
flag = False
for x in range(8):
tamanho = len(lista)
n = int(input("Digite um número inteiro: "))
if( tamanho > 0 ):
for y in range( tamanho ):
if ( n <= lista[y] ):
lista.insert( y, n )
flag = True
break
if((x == 0) or (flag == False)):
lista.append( n )
else:
flag = False
print(lista)
Final considerations
I recommend that you look into the topic ( ordering algorithms ) and try to break the head before simply copying the above code. While learning, it is always good to try hard - but try too hard - to solve the exercises / duties / challenges / etc alone so that you can learn to be self-taught and get used to a new way of thinking will come faster after a while. It will broaden your horizons well and sharpen your mind and prepare you for a time when there will be no teacher and no ready solution!