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
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:
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 toprint(col, end=' ')instead.Or, more simply:
Or, even more concisely—but probably not as simply:
You can similarly improve theMap by just creating theMap in one pass, instead of creating empty rows, then replacing them. For example:
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:
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:
You might find this a bit simpler to read, depending on your familiarity with the
joinmethod, theprintfunction, etc.: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:
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:
At any rate, these all do the same thing:
Unless those
MIN_COLUMNandMAX_COLUMNvalues are part of the requirements, I’d use a singleCOLUMNS=20variable instead—again, it avoids another common off-by-one error (that+ 1in 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:
At any rate, the advantage of this goes beyond the simplicity in
displayand not needingloadMapat all—to move the player, instead of this:You just do this:
Your entire game state is reduced from a complicated list of lists to a single integer.