Python Forum

Full Version: string index out of range
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Okay so I'm having trouble finding my problem I know the line of code that Idle is telling me that needs corrected is not the problem, but I can't seem to find what the problem is. Please let me know if you can find it or if I need to post more of my code. Thanks

The Error:
Error:
  File "/home/ryan/Desktop/CS3560/Homework_3/prog3_new.py", line 352, in print_board     line = line + board[row][col] + "  |  " IndexError: string index out of range
The code:

def print_board(board, count):
    line = ""
    if(count > 0):
        for row in range(0, 4):
            for col in range(0, 4):
                if(col == 0):
                    line = line + "  "

                if(col == 3 and row != 3):
                    line = line + board[row][col]+ "  \n------------------------\n"
                elif(col == 3):
                    line = line + board[row][col]+ "  "
                else: line = line + board[row][col] + "  |  "
                
        print line
    else:
        for row in range(0, 4):
            for col in range(0, 4):
                if(col == 0):
                    line = line + "  "
                    
                if(col == 3 and row != 3):
                    line = line + board[row][col]+ "  \n------------------------\n"
                elif(col == 3):
                    line = line + board[row][col]+ "  "
                else:
                    line = line + board[row][col] + "  |  "
            
        return line
How do you know that line is not the problem? Are you sure that board isn't getting corrupted somehow so that the rows are no longer strings of length 4?

If this were my code and I was getting that error, I would print each row before the line is generated. That way you can see exactly what row is causing the error.

Also, I'm a little confused by the if(count > 0): part. The for loops in the if and the else seem identical. The difference is whether your print the line or return it. I would calculate the line outside the if/else, and then just use the conditional to print or return as needed. Bugs breed in repeated code.
I agree that would be easier. I changed the code as seen below. Also the count is a variable I change so I can use this function to print the board for the players to see and so I can save the board in a string to be saved to a file for the players to come back later and resume their game. That line might be the problem, but I don't see how I'll try printing each line to see if I can find the problem.

def print_board(board, count):
    line = ""
    for row in range(0, 4):
        for col in range(0, 4):
            if(col == 0):
                line = line + "  "

            if(col == 3 and row != 3):
                line = line + board[row][col]+ "  \n------------------------\n"
            elif(col == 3):
                line = line + board[row][col]+ "  "
            else: line = line + board[row][col] + "  |  "
            
    if(count > 0):            
        print line
    else:   
        return line
cusick11,

please take a look at your code. There is no indentation. If you follow rules and use code tags this problem
will magically disappear.
The code didn't post correctly here. 

def print_board(board, count):
    line = ""
    for row in range(0, 4):
        for col in range(0, 4):
            print board

            if(col == 0):
                line = line + "  "
            
            if(col == 3 and row != 3):
                line = line + board[row][col]+ "  \n------------------------\n"
            elif(col == 3):
                line = line + board[row][col]+ "  "
            else: line = line + board[row][col] + "  |  "
            
    if(count > 0):            
        print line
    else:   
        return line
No, sparkz_alot put the code tags in your first post for you. Lean how to do them yourself (python in [] before the code, python in [/] after the code).
I changed your user group to User which gives you the ability to edit your posts. Please change all your posts to use python code tags
help here
I think I got it. Thanks this my first time using a forum.
def print_board(board, count):
    line = ""
    for row in range(0, 4):
        for col in range(0, 4):
            print board[row][col]

            if(col == 0):
                line = line + "  "
            
            if(col == 3 and row != 3):
                line = line + board[row][col]+ "  \n------------------------\n"
            elif(col == 3):
                line = line + board[row][col]+ "  "
            else: line = line + board[row][col] + "  |  "
            
    if(count > 0):            
        print line
    else:   
        return line
whats the arguments you are giving to the function?
You should move the print under the row loop, not the col loop, and just print board[row]. That will show you what the row is that causes the error. What you have will work, but it will be less clear.