Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8888573
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T22:04:46+00:00 2026-06-14T22:04:46+00:00

i keep getting the error IndexError: tuple out of range and i was hoping

  • 0

i keep getting the error IndexError: tuple out of range and i was hoping you guys could help me with my program

MIN_ROW = 0
MAX_ROW = 3
MIN_COLUMN = 0
MAX_COLUMN = 19

Problem seems to persist at the line 7

def display(theMap):
r = 0
c = 0
print("PLAYER MAP")
for r in range (0, (MAX_ROW + 1), 1):
    for c in range (0, (MAX_COLUMN + 1), 1):
        print(theMap[r][c])   #this line
    print()
print()

def loadMap():
theMap = []
for r in range(0,(MAX_ROW+1), 1):
    theMap.append([])
    for c in range(0,(MAX_COLUMN+1), 1):
        theMap[r].append(" ")
#           0    1    2    3    4    5    6    7    8    9   10   11    12   13   14   15   16    17   18   19 
map  [0] =   "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J"
map  [1] =   "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-"
map  [2] =   "|r|","| |", "| |", "| |", "| |", "| |", "| |", "| |", "| |", "| |", "|" 
map  [3] =   "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-"
return theMap

So im not sure how my tuple is out of range but hoping you guys can point out and help me out

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T22:04:47+00:00Added an answer on June 14, 2026 at 10:04 pm

    Writing things with explicit loops often leads to problems that are hard to debug. That’s why Python doesn’t force you to do so, and in fact encourages you not to:

    def display(theMap):
        print("PLAYER MAP")
        for row in theMap:
            for col in row:
                print(col)
            print()
        print()
    

    This still isn’t really what you want, because print(col) prints a newline. You don’t want each cell on its own line, you want spaces between the cells, and each row on its own line. To get that, you have to print(col, end=' ') instead.

    Or, more simply:

    def display(theMap):
        print("PLAYER MAP")
        for row in theMap:
            print(' '.join(row))
        print()
    

    Or, even more concisely—but probably not as simply:

    def display(theMap):
        print("PLAYER MAP")
        print('\n'.join(' '.join(row) for row in theMap))
        print()
    

    You can similarly improve theMap by just creating theMap in one pass, instead of creating empty rows, then replacing them. For example:

    def loadMap():
        return [
            ("0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J"),
            ("-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-"),
            ("|r|","| |", "| |", "| |", "| |", "| |", "| |", "| |", "| |", "| |", "|"),
            ("-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-")]
    

    In general, whenever you find yourself writing for i in range(something) in Python, you’re doing something unnecessary that you’re just going to end up debugging.

    Now, you could argue that this style doesn’t “catch errors”. But doing things with explicit loops only catches a handful of errors, and catches them in a way that’s very hard to debug. if you know your preconditions, you can usually write them much more simply and clearly:

    theMap = loadMap()
    assert(all(len(row) == len(theMap[0]) for row in theMap))
    

    Also, if you know what the output of a test case should be, you can write unit tests that verify the output.

    Anyway, even after fixing all of this, you’re still going to have some problems. For example, besides the fact that row 2 is a few columns shorter than the other rows, the individual columns in that row are 3x as big as the ones in the other rows, so it’s not going to line up at all. But once you’ve got it running, it should be easier to debug the visual stuff.

    Taking a step back, you obviously are going to be advancing the ‘r’ through row 2. In fact, all you probably need to represent the map is the width and the current position:

    def display(mapWidth, playerPosition):
        print('PLAYER MAP')
        # Display the first line. This is the header, with a label for each of the
        # mapWidth columsn, with a space between each label, and an extra space at
        # the start, like ' 0 1 2 3 4'. The ' '.join(s) creates a new string by 
        # putting a space between each element of s. The '012...XYZ'[:mapWidth] 
        # just takes the first mapWidth characters of the longer string. 
        print(' ' + ' '.join('0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'[:mapWidth]))
    
        # Display the second line. This is just a '-' for each column, with a space
        # between each, and an extra space at the start, like ' - - - - -'. The join
        # works the same as above; the '-' * mapWidth creates a string with mapWidth
        # copies of '-'.
        print(' ' + ' '.join('-' * mapWidth))
    
        # Display the third line. This is the trickiest. This is cell for each column,
        # where the first playerPosition cells are '| |', the playerPositionth is 
        # '|r|', and the rest are again '| |', with no space between them. It seemed
        # simpler to treat this as a group of playerPosition+1 '|' characters with 
        # spaces between, an 'r' character, and a group of mapWidth-playerPosition
        # '|' characters with spaces between again, but there are various different 
        # ways of printing something equivalent. The only new thing here is the 
        # end='' keyword argument--without that, each print ends with a newline.
        print(' '.join('|' * (playerPosition + 1)), end='')
        print('r', end='')
        print(' '.join('|' * (mapWidth - playerPosition)))
    
        # Fourth line, same as the second.
        print(' ' + ' '.join('-' * mapWidth))
        print()
    

    You might find this a bit simpler to read, depending on your familiarity with the join method, the print function, etc.:

    def display(mapWidth, playerPosition):
        print('PLAYER MAP')
        print(' ' + ' '.join('0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'[:mapWidth]))
        print(' -' * mapWidth)
        print(' '.join('|' * (playerPosition + 1)) +
              'r' +
              ' '.join('|' * (mapWidth - playerPosition)))
        print(' -' * mapWidth)
        print()
    

    It’s worth thinking through why, e.g., ' ' + ' '.join('-' * 8) is the same thing as ' -' * 8.

    Yet another way to do the tricky third line is:

       # Print mapWidth '|' characters, each followed by a ' ', except for the one
       # at the player position, which is followed by an 'r'. Then we still need one
       # more '|' at the end.
       print(''.join('|' + (' ' if i != playerPosition else 'r') 
                     for i in range(mapWidth) + '|')
    

    I think this is the easiest one to explain in English, but translating it to Python requires generator expressions, and ternary if expressions, both of which you probably don’t want to learn just yet—and the end result is something even an experience Python developer probably doesn’t want to read.

    Of course when in doubt, you can always pull things out into separate lines, or even separate functions, to make them more readable:

        cellsBeforePlayer = '| ' * playerPosition
        cellsWithPlayer = cellsBeforePlayer + '|r|'
        cellsToEnd = cellsWithPlayer + '| ' * (mapWidth - playerPosition)
        print(cellsToEnd)
    

    At any rate, these all do the same thing:

    >>> display(MAX_COLUMN - MIN_COLUMN + 1, 3)
    PLAYER MAP
     0 1 2 3 4 5 6 7 8 9 A B C D E F G H I J
     - - - - - - - - - - - - - - - - - - - -
    | | | |r| | | | | | | | | | | | | | | | |
     - - - - - - - - - - - - - - - - - - - -
    

    Unless those MIN_COLUMN and MAX_COLUMN values are part of the requirements, I’d use a single COLUMNS=20 variable instead—again, it avoids another common off-by-one error (that + 1 in the function call, which is very easy to forget to include when needed, or to add incorrectly when not needed).

    Or, if the map width is fixed, it’s even simpler:

    def display(playerPosition):
        print('PLAYER MAP')
        print(' ' + ' '.join('0123456789ABCDEFGHIJ'))
        print(' -' * 20)
        print(' '.join('|' * (playerPosition + 1)) +
              'r' +
              ' '.join('|' * (20 - playerPosition)))
        print(' -' * 20)
        print()
    

    At any rate, the advantage of this goes beyond the simplicity in display and not needing loadMap at all—to move the player, instead of this:

    theMap[2][playerPosition] = '| |'
    playerPosition += stepsMoved
    theMap[2][playerPosition] = '|r|'
    display(theMap)
    

    You just do this:

    playerPosition += stepsMoved
    display(playerPosition)
    

    Your entire game state is reduced from a complicated list of lists to a single integer.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I keep getting this error: line 4, in timesTwo IndexError: list index out of
I keep getting an error that states my String index is out of range
I am trying to simulate my small program and I keep getting error messages
I'm trying to install a package that uses global-linum-mode and keep getting: error Autoloading
With the following code, I keep getting error C2535 when I compile. It's complaining
I'm trying to code some stuff for a game but I keep getting error
Keep getting this error after inserting a subdatasheet into a query and trying to
Keep getting the error Arguments are not sufficiently instantiated for the multiplication by addition
I keep getting an error saying that @android:style/Widget.Holo.Light.Button.Borderless is not public and so can't
I keep getting this error and have no idea why. I googled and scanned

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.