Home > database >  Variables in for loops
Variables in for loops

Time:01-17

Hello I am new to coding and have chosen to start off with the language python 3.x. I am running into a problem as when I declare a variable and insert it in a for loop the variable is not changed.

This can be seen as after running the code below the output remains as the original value of 5.

    i = 5

    for x in range(2):
     i   1

    print(i)

CodePudding user response:

You use i = ... to give it a value, then you do not use i = ... anywhere to give it a new value, so the value never changes. You need:

i = 5

for x in range(2):
 i = i   1

print(i)

i 1 alone is not an error, but it does the calculation and throws the result away, as you did not say what else to do with it.

  •  Tags:  
  • Related