I'm not understanding loops while nested

2

I'm having trouble understanding this code, because I do not understand how it behaves in 'while' loops, it follows instructions after the code.

Obs1: I'm learning programming logic still, so       I'm "hitting head" with simple codes.

Obs2: The code is correct, I just need to understand the code flow.

Obs3: the '#' with the number in the code represents the questions.

#!/usr/bin/python3

primeira = []
segunda = []
terceira = []

while True:
    e = int(input("Digite um valor para a primeira lista (0 para terminar):"))
    if e==0:
        break
    primeira.append(e)
while True:
    e = int(input("Digite um valor para a segunda lista (0 para terminar):"))
    if e==0:
        break
    segunda.append(e)

duas_listas = primeira[:]
duas_listas.extend(segunda)

x=0
while x < len(duas_listas): #1
    y = 0
    while y < len(terceira): #2
        if duas_listas[x] == terceira[y]:
            break;
        y=y+1 #3
    if y == len(terceira): #4
        terceira.append(duas_listas[x])
    x=x+1
x=0
while x < len(terceira): #5
    print("%d: %d" % (x, terceira[x]))
    x=x+1

1 - In this first check I understood, because the list contains data, then for me it makes sense x to be smaller than the length of the list.

2 - Here, since y is 0 (zero) can be less than the length of the "third" list and the "third" list is empty ?, ie zero is less than zero?

3 - Because the code did not increment y = y + 1 where y = y + 1 is the next line after break.

4 - If the len () method counts FROM number 1 when it contains data within a list, when a list does not contain its value and zero?

5 - Here I understood, the while loop goes through each item in the "third" list and prints the value of each item.

    
asked by anonymous 31.03.2017 / 17:49

1 answer

1
  

1 - In this first check I understood, because the list contains data, then for me it makes sense x to be smaller than the length of the list.

ok

  

2 - Here, since y is 0 (zero) can be less than the length of the "third" list and the "third" list is empty ?, ie zero is less than zero?

In the first pass this is true in the second it may not be anymore since just below you can have a append in the list:

if y == len(terceira): #4
    terceira.append(duas_listas[x])
  

3 - Because the code did not increment y = y + 1 where y = y + 1 is the next line after break.

Increasing, as long as you enter this loop

  

4 - If the len () method counts FROM number 1 when it contains data within a list, when a list does not contain its value and zero?

That.

  

5 - Here I understood, the while loop goes through each item in the "third" list and prints the value of each item.

Ok.

Do a table test in the algorithm and you will understand it better. You can do with a debugger and see step by step what is happening.

    
31.03.2017 / 17:59