contig = deque()
contig.append(2)
answer = 0
while contig:
dist = contig.popleft()
val = 0
total = pow(2, val)
while total < dist:
val += 1
if total == dist:
answer += 1
val = 0
elif total > dist:
answer += 1
contig.append(dist - pow(2, val - 1))
val = 0
print(answer)
================================code below this line contains no infinite loop
contig = deque()
contig.append(2)
answer = 0
while contig:
dist = contig.popleft()
val = 0
while pow(2, val) < dist:
val += 1
if pow(2, val) == dist:
answer += 1
val = 0
elif pow(2, val) > dist:
answer += 1
contig.append(dist - pow(2, val - 1))
val = 0
print(answer)
All this can be simplified into :
zz = 1
jj = 0
while zz * jj < 10 :
jj += 1
print(zz, jj )
###############################below this line contains an infinite loop
while True:
z = 1
j = 0
r = z + j
while r < 10:
j += 1
print(z, j , r )
Can anyone provide an explanation on why it works this way? It was marked as duplicate and closed by someone, but I was chatting with a user about it and their explanation was as follows :

No disrespect, but I am not really understanding what is going on for my exmaple. They linked Variable X not updating when variables that should effect X change this post, but I am also not seeing the relationship here. At any rate, could someone provide an explanation in regards to the examples that I am giving? According to user it doesn't have anything to do pass by reference or value, Thanks a lot all. Also, the user says that total = pow(2,val) sets the value of total once and never updates it again, but why should it not update? the program does check if total < dist, thats in the while loop. As far as I can see, the issue is that it always refers to the first time I declared it. Why is this case? I also tested this in c++ and am getting the same results.
Edit* right total is not modified, but total is defined to be comprised of 2 values outside of the loop, and those values are updated, so why doesn't this update reflect in the while condition check? Just want to leave this here since if it was unclear to one user it probably is to others
I think im doing a terrible job asking my question so I just want to reclarify
while True:
z = 1
j = 0
r = z + j
while r < 10:
j += 1
print(z, j , r )
r always remains one, putting this code in an infinite loop. However, i defined r = z + j and j is getting incremented. I don't understand why the compiler is not re calculating what r is when it checks the condition
r = z + jit will addzandjand put the result inr. But it will not modify the result ofrany further, unless you give a further specific instruction to do so. The value of a variable will not be "automatically" modified or updated.