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

Asked

Viewed 54 times

1

"Make a program that, when filling a list with 8 integers, already stores them increasingly. Show 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)
  • Where did you get the code? If you didn’t understand it, I assume you didn’t develop it.

  • apparently, this variable inserted, will be True if the element has already been inserted in the list, and False if it is the last one in the list. so if it is the last element, it will be put by usefulness.

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 serves to know if you already have something in the list. only to give a complicated code the program runs backwards.

the serious code the same thing if the variable entered was already True and become False at the time of storage of the sizes and the last if could not have the not

Browser other questions tagged

You are not signed in. Login or sign up in order to post.