Python Forum
string index out of range
Thread Rating:
  • 1 Vote(s) - 3 Average
  • 1
  • 2
  • 3
  • 4
  • 5
string index out of range
#1
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
Reply
#2
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
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
Reply
#4
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.
Reply
#5
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
Reply
#6
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).
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#7
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
Recommended Tutorials:
Reply
#8
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
Reply
#9
whats the arguments you are giving to the function?
Recommended Tutorials:
Reply
#10
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to fix list index out of range longmen 26 5,693 Apr-27-2022, 05:46 PM
Last Post: deanhystad
  list index out of range OliverG 3 2,289 Sep-03-2021, 12:04 AM
Last Post: itsmycode
  List index out of range when turning CSV into dict ranbarr 15 6,281 May-12-2021, 10:38 AM
Last Post: ranbarr
  Use range to add char. to empty string johneven 5 13,742 Mar-24-2020, 11:52 AM
Last Post: mbharatkumar
  list index out of range mcgrim 2 2,859 May-25-2019, 07:44 PM
Last Post: mcgrim
  IndexError: list index out of range abdullahali 4 3,800 Jan-17-2019, 07:54 AM
Last Post: buran
  String index out of range felie04 2 5,479 Aug-17-2018, 11:18 PM
Last Post: felie04
  Accessing data in zip - Index out of range pythoneer 24 12,571 Mar-15-2018, 06:19 PM
Last Post: buran
  "List index out of range" for output values pegn305 3 5,252 Nov-26-2017, 02:20 PM
Last Post: heiner55
  list index out of range DrPengin 1 3,659 Nov-09-2017, 08:35 PM
Last Post: gruntfutuk

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020