Rasberry
Rasberry

Reputation: 13

Why does the i in second loop counts as 1 less than the given input by the user?

Second i in loop counts as 1 less than the given input by the user for some reason. I dont know where the i changes it's value

One way i tried to fix it is by changing

for i in range (int(i))

to

for i in range (int(i)+1)

Does fix the problem but idk why it is needed

i = input()
AliceShapeSequence = []
BobShapeSequence = []

for i in range (int(i)):
        AliceShape = input()
        AliceShapeSequence.append(AliceShape)
    
for i in range (int(i)):
        BobShape = input()
        BobShapeSequence.append(BobShape)
    
print(AliceShapeSequence)
print(BobShapeSequence)

Excepted both lists to have 3 values but AliceShapeSequence has 3 and BobShapeSequence has 2 (depends on the input i used 3 for this example)

Upvotes: 1

Views: 623

Answers (4)

Quelklef
Quelklef

Reputation: 2147

This is actually quite an interesting question. Let's step through what's happening here.

i = input()

Say your user enters 3. Now i is 3, as expected.

for i in range (int(i)):

First, range(int(i)) is called. Python's range, when called with one argument, creates a range from 0 (inclusive) to that argument (exclusive). Since i is 3, this creates a range containing the values 0, 1, and 2.

Now, you start iterating, with an iteration variable i.i is first set to 0, then the code block is run, 1, and the code block is run, then 2, and the code block is run for a final time.

However, there's a trick. Due to Python's scoping rules, you iteration variable i is actually the same variable as the global variable i. So during iteration you were updating the global i value to 0, then 1, then 2. So when we start the next loop:

for i in range (int(i)):

range(int(i)) is range(2), which creates a range with the elements 0 and 1—not what you wanted.

To fix this, change the iteration variables to something else—perhaps j, as another answer suggests.

Upvotes: 2

Fabian
Fabian

Reputation: 1150

The variable i is global any python function or class defined in this module is able to access this variable:

Your key value and your input variables are at the same scope.

So you input 3 and the loops starts counting at 0 to after the loop i is counted up to 2 after the first loop so the loop will start with 2:

i = input()
AliceShapeSequence = []
BobShapeSequence = []

for x in range (int(i)):
        AliceShape = input()
        AliceShapeSequence.append(AliceShape)

for x in range (int(i)):
        BobShape = input()
        BobShapeSequence.append(BobShape)

print(AliceShapeSequence)
print(BobShapeSequence)

Just change upcounting key to other variable name you are fine.

To make it easy to understand just run:

i = 3
AliceShapeSequence = []
BobShapeSequence = []

for i in range (int(i)):
        AliceShape = i
        AliceShapeSequence.append(AliceShape)
        print("first loop: i = {0} after {1} loop cycle".format(i, i+1))

for i in range (int(i)):
        BobShape = i
        BobShapeSequence.append(BobShape)
        print("second loop: i = {0} after {1} loop cycle".format(i, i+1))


print(AliceShapeSequence)
print(BobShapeSequence)

Upvotes: 0

nohnce
nohnce

Reputation: 61

The i in the for loop is updating every time it runs.

range(5) // gives you 0-4

i is set to 4 on the last cycle of the loop. Use a different variable name.

Upvotes: 0

kleinbottle4
kleinbottle4

Reputation: 151

I ran your code, but changed for i in range(int(i)) into for j in range(int(i)) for both cases and then it behaved as expected.

You shouldn't use the same variable names, otherwise you may have issues with unexpected side effects.

Upvotes: -1

Related Questions