Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 connect 4 unknown erroer
#1
This is the start of a 4 in a row game / connect 4. I am experiencing the error:

line 10, in is_valid_location
if board[bottom_row][column] == 0:
IndexError: index 5 is out of bounds for axis 0 with size 5


I would really appreciate some help thanks.
import numpy as np
turn = 0
game_over = False

def board_create(column, row):
    board = np.zeros((row,column))
    return board

def is_valid_location(column_choice, board, bottom_row):
    if board[bottom_row][column] == 0:
        return False
    else:
        return True

row = 5
column = int(input("How many columns: "))
board = board_create(column, row)

while not game_over:
    if turn == 0:
        invalid = True
        bottom_row = row
        while invalid == True:
            column = str(column)
            column_choice = int(input("Player 1 make your selection (1-"+column+"):"))
            column = int(column)
            invalid = is_valid_location(column_choice, board, bottom_row)
    else:
        while invalid == True:
            column = str(column)
            column_choice = int(input("Player 2 make your selection (1-"+column+"):"))
            column = int(column)
            invalid = is_valid_location(column_choice, board, bottom_row)
    turn = turn + 1
    turn = turn % 2

Quote
#2
(Aug-16-2019, 10:27 AM)Help_me_Please Wrote: IndexError: index 5 is out of bounds for axis 0 with size 5
axis is with size 5, so indexes are from 0 to 4
Quote
#3
What does that mean? and so what do I need to change?
Quote
#4
(Aug-16-2019, 10:43 AM)Help_me_Please Wrote: What does that mean? and so what do I need to change?

There are zero-based index and there is finger-based index. Formula for converting:

finger-based index - 1 = zero-based index
zero-based index + 1 = finger-based index

Users count with their fingers, but for Python you need to convert it into zero-based index.
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Quote
#5
What do I change the line too, to avoid this issue.
Quote
#6
you take index as input from user and ask for number between 1 and column. Because indexes are zero-based, you need a number between 0 and column-1. It's up to you how you will achieve that - ask the user or post-process the user input before using it in your code that raise IndexError.
Quote
#7
Note also that the function is_valid_location can be simplified to, e.g. return not board[bottom_row][column] == 0. Since the expression board[bottom_row][column] == 0 already evaluates to a boolean, the if and else are unnecessary.
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Unknown error in pygame :( TheDovah77 1 934 Apr-14-2019, 10:22 PM
Last Post: metulburr
  Unknown syntax error (Im new to this) reasonablelevel 2 775 Jul-25-2018, 11:59 AM
Last Post: reasonablelevel
  ValueError: unknown url type: ' ' ozzyk 3 6,943 Nov-26-2017, 05:01 PM
Last Post: heiner55
  Unknown Syntax Error AlwaysNew 6 1,437 Sep-09-2017, 01:57 AM
Last Post: AlwaysNew
  Unknown output brzo 3 1,527 Jun-24-2017, 04:16 PM
Last Post: Larz60+
  unknown encoding: MS950_HKSCS rchiu5hk 4 2,393 Jan-26-2017, 09:26 PM
Last Post: Larz60+

Forum Jump:


Users browsing this thread: 1 Guest(s)