Apr-07-2021, 05:51 PM
(Apr-07-2021, 05:14 PM)bowlofred Wrote: First,list
anditer
are names of classes in python. You shouldn't use them as variable names or you hide the ability to use the class namespaces.
You don't have anything namedoldmatrix
in your code, but I can guess what you've done is assumed that assigning a list copies the data rather than just a reference to it. (Please put your code in "python" tags so that the indentation is preserved).
I've made some other code to show how this can affect you and one way to avoid it.
baselist = [0, 0, 0, 0] working_list = baselist # copies the object, not the elements inside working_list[2] = "XX" print(f"{working_list=}") print(f"{baselist=}") print() # Note "baselist" has changed without explicitly doing so because it is the same object # as working_list. baselist = [0, 0, 0, 0] working_list = baselist.copy() # This is a copy and the two objects are different working_list[2] = "XX" # doesn't change baselist now. print(f"{working_list=}") print(f"{baselist=}")
Output:working_list=[0, 0, 'XX', 0] baselist=[0, 0, 'XX', 0] working_list=[0, 0, 'XX', 0] baselist=[0, 0, 0, 0]
First, thank you, you are super helpful.
I indeed meant basematrix.
I made some changes according to your advice. Still doesn't;t work, basematrix get changed. I'm sure I'm doing a foolish mistake but I can't find it,
# Online Python compiler (interpreter) to run Python online. # Write Python 3 code in this online editor and run it. import random print("Hello world") bingo = 0 checkbingo = 0 basematrix = [[0,0,0,0,0],[0,0,0,0,0],[0,0,1,0,0],[0,0,0,0,0],[0,0,0,0,0]] matrix = [[0,0,0,0,0],[0,0,0,0,0],[0,0,1,0,0],[0,0,0,0,0],[0,0,0,0,0]] baselist = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24] punchlist = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24] for runningtimes in range(2): print("runningtimes =", runningtimes) print("basematrix = ", basematrix) print("matrix = ",matrix) #random punch for punchnum in range(25): n = random.randint(0, len(punchlist)-1) i = punchlist[n] matrix[round((i-i%5)/5)][round(i%5)] = 1 punchlist.pop(n) #check bingo for x in range(5): for y in range(5): checkbingo = checkbingo + (matrix[x][y]) if checkbingo == 5: bingo = 1 checkbingo = 0 if bingo == 0: for y in range(5): for x in range(5): checkbingo = checkbingo + (matrix[x][y]) if checkbingo == 5: bingo = 1 checkbingo = 0 print(punchnum," = ", bingo) print("basematrix = ", basematrix) matrix = basematrix.copy() print("matrix = ",matrix) punchlist = baselist.copy() print("punchlist =", punchlist)