The code is right, but could anyone tell me how True and False works in detail in this question (step-by-step)?

1

"Make a program that, when you populate a list with 8 integers, store it in a growing fashion. Display the resulting list each time a value is stored"

 lista = []

for x in range(8):
    n = int(input("Digite um número inteiro: "))

    inseriu = False
    for i in range(len(lista)):
        if n<lista[i]:
            lista.insert(i, n)
            inseriu = True
            break

    if not inseriu:
        lista.append(n)

    print(lista)
    
asked by anonymous 10.11.2017 / 01:15

1 answer

0

If I understand correctly you want to know how True and False works right?

The operation is as follows:

lista = [] #sua lista não tem nada.
for i in range(8):
    n = int(input("digite um número")
    inserido = False #Variavel para saber se já tem números na lista
    for j in range(len(lista)): # lista ainda não tem nada portanto esse loop não será executado.
        if n < lista[j]:
            lista.insert(i ,n)
            inserido = True
    if not  inserido: #por não ter entrado no loop anterior inserido será False e o contrário True por causa do not
        lista.append(n)
  

Explanation: The inserted variable is used to find out if you already have anything in the list.   only to give a complicated code in the program does the opposite.

     

The code would be the same if the inserted variable was already True and becomes False at the time of storing the sizes and the last if could not have not

    
10.11.2017 / 15:52