pq the input x iteration DOES NOT interfere with the while loop x iteration, where it is the same variable?

Asked

Viewed 43 times

0

I am learning programming logic with Python and I don’t understand how the input x DOES NOT interfere with the iteration of the x from the while loop, which is the same variable. Please explain to me, 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] ))
  • that x of input?

1 answer

1


What happens is that when you do any operation with a variable, it does not change 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 is what changes the value. Because at first it simply returns the summed value but does not change the original value of the variable because it has no assignment.

Browser other questions tagged

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