0

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)

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.