user13825319
user13825319

Reputation:

Python: Why is my `for` loop being skipped?

I have this code:

for i in range(40):
    print("execution:", i, end="    ")
    if player_y - i >= 0:
        if roomMap[player_y - i][player_x] == 1:
            roomMap[player_y - i][player_x] = 3

The variables player_x, player_y and the list roomMap are already defined, but it does nothing and I am unsure what's happening.

Upvotes: 0

Views: 1225

Answers (1)

Ziad Ali
Ziad Ali

Reputation: 56

There are multiple reasons why your loop would not be running

  • You had a conditional statement (if) surrounding it and the code happened not to run through that branch
  • You maybe had a return statement before getting into that loop and your code terminated

Steps you can take to resolve that:

  • Try to use the debugger and see if the code goes inside this loop
  • You can also use "print()" statements to caveman debug it, maybe use print("I am inside the loop") see if you can print that in the console from inside the loop.

Upvotes: 0

Related Questions