Need help understanding how the machine reads this code

# 🚨 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 👇

horizontal = int(position[0])
vertical = int(position[1])

map[vertical - 1][horizontal - 1] = "X" 

#Write your code above this row 👆

# 🚨 Don't change the code below 👇
print(f"{row1}\n{row2}\n{row3}")

Question: So I think I understand how this code generally works but I am not sure why the horizontal comes second in the "map[vertical – 1][horizontal – 1] = "X"
" line. Wouldn’t the horizontal come first since it is the first index [0] and horizontal is being read first (horizontal needs to come first to determine column before we determine the vertical row in this example)?

Edit: I’m asking for help because I genuinely don’t know. I’ve tried looking over it and I’ve tried researching it.

>Solution :

Consider

  • map[0] returns the same object as row1, map[1] returns the same object as row2, and map[2] returns the same object as row3.
  • Rows are (traditionally) visualized as a vertical stack, one on top of each other, the same as one line above the other in the original code.

Thus

 a_row = map[vertical - 1]
 a_value = a_row[horizontal - 1]

This can be combined as

 a_value = map[vertical - 1][horizontal - 1]

And with assignment of a specific value

 map[vertical - 1][horizontal - 1] = "X"

Leave a Reply