because the iteration of the x of the input does NOT interfere in the iteration of the x of the while loop, and is it the same variable?

0

I'm learning programming logic with Python and I do not understand how the x of the input does NOT interfere with the iteration of the while loop, since it is the same variable. Please, I would like you to explain, valew !!!

#!/usr/bin/python3

numeros = [0,0,0,0,0]

x = 0

while x < 5: 
    numeros[x] = int(input("Números %d:" %(x + 1) )) 
    x += 1

while True:
    escolhido = int(input("Que posição você quer imprimir (0 para sair): "))
    if escolhido == 0:
        break
    print("Você escolheu o número: %d" %( numeros[escolhido -1] ))
    
asked by anonymous 29.03.2017 / 17:42

1 answer

1

What happens is that when you do any operation with a variable, it does not change its value.

x = 1
x + 1 // 2
print(x) // 1

To change the value of a variable you need to assign a new value to it.

x = 1
print(x) // 1
x = x + 1 // mesmo que x += 1
print(x) // 2

So only the second x changes the value. Because in the first it simply returns the added value but does not change the original value of the variable because it has no assignment.

    
29.03.2017 / 18:12