Davina
Davina

Reputation: 37

IndexError: list index out of range? Python

So I have created a game that places treasure chests and bandits within a grid and allows the player to move through it. Essentially, if the player lands on a treasure chest, which are randomly placed within the grid, their coin total goes up by 10, and if the player lands on a bandit, the coin total is reset to 0.

However, I have attempted to get the program to turn a specific treasure chest into a bandit if it has been visited 3 times. I have used the lists visit[] and visited[] to do so, however I keep getting the error:

IndexError: list index out of range

and I don't know why? I have attached all my code for reference and outlined the section that seems to be causing the problem. How do I get rid of the error so that the chest turns into a bandit after it's third visit?(on it's fourth)

Traceback: 
line 130, in <module>
    visits[i] += 1
IndexError: list index out of range

choice = 0
b = 0
player_location = ' X '
x = 8
y = 0
coins = 0
bandits = 5
treas_chests = 10
a = 1
import random
x_move = 0
y_move = 0
no_of_bands = 5
size_of_grid = 8
chests = []
bandits = []
size_of_grid_n = 0
visits = []
visited = []
def menu(): 
    print('If you would like to play the Treasure Hunt , press 1') 
    choice = input('If not, press any key to exit \n') 
    if choice == '1': 
        print('Great! You have made the right choice :)') 
    else: 
        print('Goodbye.') 
        quit() 
menu() 
def board(): 
    new_board = [ ]
    top_row = [' 1 ',' 2 ',' 3 ',' 4 ',' 5 ',' 6 ',' 7 ',' 8 ']

    new_board.append(top_row)

    for x in range(0, 8):
        row = [' 0 '] * size_of_grid
        new_board.append(row)

    return new_board

def print_board(b):
  row_numbers = [' ', '1', '2', '3', '4', '5', '6', '7', '8']
  i = 0
  for row in b:
    print (row_numbers[i], ''.join(row))
    i = i + 1
current_x_loc = 0
current_y_loc = size_of_grid

def update_board(b, current_y_loc, current_x_loc):
    #b[current_y_loc-y_move][current_x_loc+x_move] = player_location
    while (-1 >= current_y_loc-y_move) or (current_y_loc -y_move > size_of_grid):
        print('INVALID INPUT')
        get_move()
    while (-1 >= current_x_loc + x_move) or (current_x_loc + x_move > size_of_grid):
        print('INVALID INPUT')
        get_move()
    b[current_y_loc - y_move][current_x_loc + x_move] = player_location
    current_y_loc -= y_move
    current_x_loc += x_move
    print("current location = ", current_x_loc, current_y_loc)
    return current_y_loc, current_x_loc

def chests_and_bandits():
    num = 0
    while num < treas_chests:
        y_c = random.randint(1, size_of_grid)  
        x_c = random.randint(1, size_of_grid)
        location = [y_c, x_c]
        while (location in location) or (location == [size_of_grid, 0]):
            y_c = random.randint(1, size_of_grid)
            x_c = random.randint(1, size_of_grid)
            location=[y_c, x_c]
        chests.append(location)
        num = num + 1

    num = 0
    print(chests)
    while num < no_of_bands:
        y_b = random.randint(1, size_of_grid)
        x_b = random.randint(1, size_of_grid)
        location=[y_b, x_b]
        while (location in location) or (location in chests) or (location == [size_of_grid, 0]):
            y_b = random.randint(1, size_of_grid)
            x_b i= random.randint(1, size_of_grid)
            location = [y_b, x_b]
        bandits.append(location)
        num = num + 1
    print(bandits)

while a == 1:
    chests_and_bandits()
    def get_move():
        advice = 'Please enter your move in two integers, vertical, then horizontal, separated by a space.  Use positive numbers for up and right, negative for down and left.'
        example = 'For example, an input of \'2 2\' would be 2 moves vertically, and 2 moves horizontally.'
        move = input(advice + example)
        coor=move.split()
        while len(coor) != 2:
            print('Invalid input- too many or too few co-ordinates')
            print('')
            advice = 'Please enter your move in two integers, vertical, then horizontal, separated by a space.  Use positive numbers for up and right, negative for down and left.'
            example = 'For example, an input of \'2 2\' would be 2 moves vertically, and 2 moves horizontally.'
            move = input(advice + example)
            coor = move.split()
        move = move.split()
        y_move, x_move = move
        x_move = int(x_move)
        y_move = int(y_move)
        return x_move, y_move

    while True:
        new_board = board()
        current_y_loc, current_x_loc = update_board(new_board, current_y_loc, current_x_loc)
        print_board(new_board)
        print(' ')
        print(current_x_loc, current_y_loc)
        cur_loc = [current_x_loc, current_y_loc]

The section below is where the lists that are bringing up the error are used

        for i in range (0, len(chests)):
            if cur_loc == chests[i]:
                coins += 10
                print('You have found a treasure chest')
                print('Number of coins: ',coins)
                print('')
                visits[i] += 1
                if visits[i] > 3:
                    visited[i] = True
                    bandits.append(chests[i])
                    var = chests[i]
            for i in range(0, len(visited)):
                chests.remove
                if visits[i] == 3:
                    treas_chests -= 1
                    no_of_bands += 1
                    bandits.append(chests[i])
                    var = chests[i]
        for i in range (0, len(bandits)):
            if cur_loc == bandits[i]:
                coins = 0
                print('You have hit a bandit')
                print('Number of coins: ', coins)
                print('')


        x_move, y_move = get_move()

Upvotes: 0

Views: 740

Answers (1)

VMAtm
VMAtm

Reputation: 28345

You do the iteration over range (0, len(chests)), and after that get an error in line visits[i] += 1. Looks like chests and visits hasn't equal length. I can't see any appends to the visits list, so it's basically empty.

Also, you are mixing up the counters in a nested loop:

for i in range (0, len(chests)):
    ...
    # you override the i value here
    for i in range(0, len(visited)):

Just do inner loop other other counter, e.g. j:

for i in range (0, len(chests)):
    ...
    for j in range(0, len(visited)):

Also you're using the len(visited), but go over visits[i], but there is no guarantee that these two arrays are equal length.

Upvotes: 1

Related Questions