I am trying to do an exercise learning Python for nested lists and I the way it works is that you have a grid, and then based on what you enter, it will replace that position with an "X". You also need to do it while entering a 2 digit number. I was able to get to a point where the input was turned into a list and was able to make sure they were integers too.
# 🚨 Don't change the code below 👇
row1 = ["⬜️","️⬜️","️⬜️"]
row2 = ["⬜️","⬜️","️⬜️"]
row3 = ["⬜️️","⬜️️","⬜️️"]
map = [row1, row2, row3]
print(f"{row1}\n{row2}\n{row3}")
position = input("Where do you want to put the treasure? ")
# 🚨 Don't change the code above 👆
#Write your code below this row 👇
#Take the 2-digit number and split into the column and row numbers
#Also now integers to find the indexes
column_num = int(position[0])
row_num = int(position[1])
#Take the 2 numbers, and use to make where to mark the X
new_list = [column_num, row_num]
new_column = new_list[0]
new_row = new_list[1]
x = [new_column] + [new_row]
print(new_list)
print(x)
# Replace the new_list list, which have the indexes, into X
map[new_list] = "X"
#Write your code above this row 👆
# 🚨 Don't change the code below 👇
print(f"{row1}\n{row2}\n{row3}")
I get this error:
Traceback (most recent call last):
File "C:\Users\*******\IdeaProjects\_100DaysOfCodev2\.idea\Day 4 - Beginner - Randomisation and Python Lists\6- [Exercise] - Treasure Map.py", line 27, in <module>
map[new_list] = "X"
TypeError: list indices must be integers or slices, not list
I have tried to change the list into an "int" but then it tells me not tuple instead of list.
Anyone got any ideas? I want to understand what I am missing here, instead of going back and playing the solution.
I tried to separate the values of the new list so they would be integers but I needed them to be a set of index but it won't work because it becomes a list again. I have tried summoning different things from "map" nested list and I get index errors that it is out of index range as well. Also we are not in "for, in" yet or defining functions yet. Traceback (most recent call last): File "C:\Users\e792977\IdeaProjects_100DaysOfCodev2.idea\Day 4 - Beginner - Randomisation and Python Lists\6- [Exercise] - Treasure Map.py", line 27, in map[new_list] = "X" TypeError: list indices must be integers or slices, not list
You need to identify the 2 indices explicitly:
map[new_list[0]][new_list[1]] = "X"
or in the context of your current solution something like:
# 🚨 Don't change the code below 👇
row1 = ["⬜️","️⬜️","️⬜️"]
row2 = ["⬜️","⬜️","️⬜️"]
row3 = ["⬜️️","⬜️️","⬜️️"]
map = [row1, row2, row3]
print(f"{row1}\n{row2}\n{row3}")
position = input("Where do you want to put the treasure? ")
# 🚨 Don't change the code above 👆
#Write your code below this row 👇
x_index = int(position[0]) - 1 ## assume that users will use 1 indexing
y_index = int(position[1]) - 1
map[y_index][x_index] = "X"
#Write your code above this row 👆
# 🚨 Don't change the code below 👇
print(f"{row1}\n{row2}\n{row3}")
Note:
As others have pointed out, map
is a poor choice for a variable name as it will hide the python function map()
. You can find a full list of such here : Built-in Functions
Secondly, python lists are 0-indexed
and if our user is not expecting that there might be confusion. This code takes their requested row and column and converts it to 0-indexed
assuming that they expected row 1
to be the "first" row.
Finally, I expect that when asked for coordinates that I give them in the following order (xy
):
However, each "row" of data is the i-th y-offset
and each cell of data in a given row is the j-th x-offset
so when we go to set our "X" we will index into our data via map[y_index][x_index]
and not map[x_index][y_index]
as one might intuit.