Okay. So I'm in Programing fundamentals and for our final project we have to design a game with a map. I created a program that creates a list of ten lists with ten items in each. However when I try to change a single item in one list (for example mapGrid[4][5] = "i") it changes all of the 6th items in all of the lists.
Here is the code I'm using to test it:
import random
def createMap():
mapGrid = []
col = []
randx = 0
randy = 0
i = 0
for x in range(0,10):
col += "#"
for y in range(0,10):
mapGrid.append(col)
while i < 10:
randx = random.randint(0,9)
randy = random.randint(0,9)
if mapGrid[randx][randy] != "i":
mapGrid[randx][randy] = "i"
i += 1
return mapGrid
def printMap(x,y,mapGrid):
mapGrid[x][y] = "0"
print("",mapGrid[x-1][y+1],mapGrid[x][y+1],mapGrid[x+1][y+1],"\n",
mapGrid[x-1][y],mapGrid[x][y],mapGrid[x+1][y],"\n",
mapGrid[x-1][y-1],mapGrid[x][y-1],mapGrid[x+1][y-1])
examp = createMap()
print(examp)
print("")
printMap(4,4,examp)
and the result I keep getting is:
[['i', 'i', 'i', 'i', 'i', 'i', 'i', 'i', 'i', 'i'], ['i', 'i', 'i', 'i', 'i', 'i', 'i', 'i', 'i', 'i'], ['i', 'i', 'i', 'i', 'i', 'i', 'i', 'i', 'i', 'i'], ['i', 'i', 'i', 'i', 'i', 'i', 'i', 'i', 'i', 'i'], ['i', 'i', 'i', 'i', 'i', 'i', 'i', 'i', 'i', 'i'], ['i', 'i', 'i', 'i', 'i', 'i', 'i', 'i', 'i', 'i'], ['i', 'i', 'i', 'i', 'i', 'i', 'i', 'i', 'i', 'i'], ['i', 'i', 'i', 'i', 'i', 'i', 'i', 'i', 'i', 'i'], ['i', 'i', 'i', 'i', 'i', 'i', 'i', 'i', 'i', 'i'], ['i', 'i', 'i', 'i', 'i', 'i', 'i', 'i', 'i', 'i']]
i i i
0 0 0
i i i
instead of there being ten single i's and one '0' it becomes all i's and every 5th item in each list is a 0.
How do I fix it so I only change a single item instead of an item in every list?