Python Forum
Index error using pop in nested loop
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Index error using pop in nested loop
#1
Hello. This is my first post, so I hope I'm not screwing this up too badly. I'm trying to get back into Python and coding in general after a 10ish year hiatus by making a snake game.

I'm creating a list of directions and a list of wait times for each snake body segment. When wait is 0 the snake body segments update their direction according accompanying list of directions. This works as intended until I attempt to clear the lists of unnecessary data by calling pop(). I'm assuming what's happening is because the list is being popped during the loop, the next iteration doesn't have the correct ranges, but I can't think of a clean alternative way to clean the list of used data.

Any insight would be appreciated. Thank you!

    def change_direction(self, new_direction):
        if new_direction != self.snake_head.direction:
            self.snake_head.direction = new_direction
            for i in range(len(self.snake_body)):
                self.snake_body[i].wait.append(i+1)
                self.snake_body[i].directions.append(new_direction)

    def traffic_control(self):
        for x in range(len(self.snake_body)):
            for y in range(len(self.snake_body[x].wait)):
                self.snake_body[x].wait[y] -= 1
                if self.snake_body[x].wait[y] == 0:
                    self.snake_body[x].change_direction_body(self.snake_body[x].directions[y])
                    #list.pop(self.snake_body[x].wait, y)
                    #list.pop(self.snake_body[x].directions, y)
Reply


Messages In This Thread
Index error using pop in nested loop - by PerksPlus - Mar-27-2019, 10:19 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  pyscript index error while calling input from html form pyscript_dude 2 974 May-21-2023, 08:17 AM
Last Post: snippsat
  Index error help MRsquared 1 762 May-15-2023, 03:28 PM
Last Post: buran
  Big O runtime nested for loop and append yarinsh 4 1,376 Dec-31-2022, 11:50 PM
Last Post: stevendaprano
  Read nested data from JSON - Getting an error marlonbown 5 1,358 Nov-23-2022, 03:51 PM
Last Post: snippsat
  Replace for loop to search index position illmattic 5 1,274 Sep-03-2022, 04:04 PM
Last Post: illmattic
  Nested for loops - help with iterating a variable outside of the main loop dm222 4 1,572 Aug-17-2022, 10:17 PM
Last Post: deanhystad
  I'm getting a String index out of range error debian77 7 2,333 Jun-26-2022, 09:50 AM
Last Post: deanhystad
  For loop index out of bounds armitron121 2 2,671 Feb-08-2022, 04:23 PM
Last Post: armitron121
  Python Error List Index Out of Range abhi1vaishnav 3 2,302 Sep-03-2021, 08:40 PM
Last Post: abhi1vaishnav
  Index error - columns vs non-column Vinny 3 4,914 Aug-09-2021, 04:46 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020