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

Life of Brian: Conjugate the verb, "to go" !
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

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Invalid JSON payload received. Unknown name “”: Root element must be a message." hellraiser 4 151 Aug-18-2019, 03:41 PM
Last Post: hellraiser
  Trying to write an app to connect to a mysql DB gizmo 0 141 Jul-02-2019, 12:00 PM
Last Post: gizmo
  Connect to database OVH Serferi 0 217 Apr-26-2019, 07:34 AM
Last Post: Serferi
  Unknown error in pygame :( TheDovah77 1 156 Apr-14-2019, 10:22 PM
Last Post: metulburr
  Connect a Teradata DB to Python OscarBoots 10 903 Jan-31-2019, 10:23 PM
Last Post: OscarBoots
  Python connect to Sybase IQ FORTITUDE 1 741 Jan-24-2019, 02:14 AM
Last Post: micseydel
  Connect to SFTP to read cvs files arunlal 1 346 Nov-20-2018, 08:32 AM
Last Post: buran
  Sample code to connect DB using parameter values nithin 2 473 Oct-15-2018, 01:44 AM
Last Post: nithin
  Connect 4 problem Darbandiman123 1 408 Oct-03-2018, 05:10 PM
Last Post: ichabod801
  Unknown syntax error (Im new to this) reasonablelevel 2 649 Jul-25-2018, 11:59 AM
Last Post: reasonablelevel

Forum Jump:


Users browsing this thread: 1 Guest(s)