string index out of range - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Homework (https://python-forum.io/forum-9.html) +--- Thread: string index out of range (/thread-2268.html) |
string index out of range - cusick11 - Mar-03-2017 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: 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 RE: string index out of range - ichabod801 - Mar-03-2017 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.
RE: string index out of range - cusick11 - Mar-03-2017 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 RE: string index out of range - Larz60+ - Mar-03-2017 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. RE: string index out of range - cusick11 - Mar-03-2017 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 RE: string index out of range - ichabod801 - Mar-03-2017 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). RE: string index out of range - metulburr - Mar-03-2017 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 RE: string index out of range - cusick11 - Mar-03-2017 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 RE: string index out of range - metulburr - Mar-03-2017 whats the arguments you are giving to the function? RE: string index out of range - ichabod801 - Mar-03-2017 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. |