Python Forum
how can a variable change if I haven't changed it?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
how can a variable change if I haven't changed it?
#4
(Apr-07-2021, 05:14 PM)bowlofred Wrote: First, list and iter 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 named oldmatrix 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)
   
    
  
Reply


Messages In This Thread
RE: how can a variable change if I haven't changed it? - by niminim - Apr-07-2021, 05:51 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Comparing two Pandas df’s and returning only changed records hobbycoder 1 706 Oct-29-2024, 01:55 PM
Last Post: deanhystad
  Help with writing monitored data to mysql upon change of one particular variable donottrackmymetadata 3 1,413 Apr-18-2024, 09:55 PM
Last Post: deanhystad
  The behavior of tune model has changed Led_Zeppelin 5 6,042 Oct-21-2021, 06:52 PM
Last Post: jefsummers
  Change variable value during a while loop? penahuse 2 5,312 Nov-15-2020, 11:53 PM
Last Post: penahuse
  Change variable in an outside file ebolisa 5 4,350 Nov-11-2020, 04:41 AM
Last Post: ebolisa
  Change name of variable samuelbachorik 2 2,788 Aug-10-2020, 02:34 PM
Last Post: deanhystad
  Python - change variable type during program execution ple 1 3,267 Apr-12-2020, 08:43 AM
Last Post: buran
  I haven't a clue how to implement this logic 357mag 3 2,927 Apr-02-2020, 04:35 PM
Last Post: 357mag
  trying to change variable value with a def pythonbegginer 11 6,292 Mar-07-2020, 02:54 PM
Last Post: pythonbegginer
  RuntimeError: dictionary changed size during iteration Shreeniket987 3 6,026 Jun-01-2019, 01:22 PM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020