简体   繁体   中英

Python replace element on a board with another?

I am currently working on program, and I have got pretty much everything down so far except for the part where the user gets to place pieces inside. I've been trying to debug it and find what I am doing wrong, but I can't seem to get it.

Example of what I am trying to do:

xxxx
xxxx
xxxx
xxxx

#I want to put a piece (ex: o) in column 1

xxxx
xxxx
xxxx
oxxx

The error I'm getting is IndexError: list index out of range .

def gameMake(board, rows, columns):

    while emptySp(): #already written (checks if the space is available for the user to put in their token)


        print("Player 1")
        mycol = input("Please choose a column (1-" + str(columns))

        if coluser >= columns:
            mycol = input("Please choose a column:")

        else:
            rowuse = rows
            x = int(rowuse)
            while x == 1:
                if board[x][coluser] == board[rows][columns]:
                    #print the board using print function 

I am trying to move on so I can check for a winner and print the board but this code so far doesn't work! Do I need to show all of my code to get any help or is snippet of where i'm having problems okay? Everything else in my code works fine!

Can anyone point out my mistake and help me?

The reason you're getting the error is because you're handing your lists tuples and you can only use integers as list indexes. Hence the error TypeError: list indices must be integers, not tuple :

# board[(x, coluser)]
type((x, coluser)) == tuple

I assume you are trying to search through a 2d list. In which case, you actually pass in two seperate indexes. Ie

board[x][coluser]

This is because a 2d list (like your board) is actually just lists inside of lists.

board = [['0a', '0b', '0c', '0d'],
         ['1a', '1b', '1c', '1d'],
         ['2a', '2b', '2c', '2d'],
         ['3a', '3b', '3c', '3d']]

So you first get the row you want:

board[1] == ['1a', '1b', '1c', '1d']

And then the element in that row:

board[1][2] == '1c'
def gameMake(board, rows, columns):

    while emptySp(board, rows, columns): #already written (checks if the space is available for the user to put in their token)
        print("Player 1")
        mycol = input("Please choose a column (1-" + str(columns))
        coluser = int(mycol)
        if coluser >= columns:
            mycol = input("Please choose a column to place your piece in (1-" + str(columns))
        else:
            rowuse = (rows - 1)
            x = int(rowuse)
            while x >= 1:
                if board[x][coluser-1] == board[rows][columns]:
                    board[x][coluser-1] == PONE #PONE = "o" (the token)

The only thing is that two-dimensional lists are indexed by using two indices instead of a tuple. I'm supposing your else goes with the if , fixing the indentation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM