1

So the other day I was stuck on a problem because of a typo. Instead of iterating through my nested loop with i += 1 I was using i=+1. I didn't notice this until I started printing the number of steps and saw it was printing step 1 continuously. The plots I was getting therefore weren't making any sense.

However what I don't understand is why I got any plots at all, and the code wasn't stuck in an infinite loop? Also, I should only have been calculating data after halfway through the number of steps, so I don't understand how I had any data at all. Or does i =+ 1 mean something else? I can't seem to find much information on i=+1 at all online

Here's a condensed version of the original code:

for temp in np.linspace(1.0,4.0,num=100):   
    energyarray = []
    for step in np.arange(0, sw*2):    
        for i in range(n-1):
           for j in range(n-1):

            H_old = -J*matrix[i,j]*(matrix[i,j-1] + matrix[i,j+1] + matrix[i-1,j] + matrix[i+1,j])
            H_new = J*matrix[i,j]*(matrix[i,j-1] + matrix[i,j+1] + matrix[i-1,j] + matrix[i+1,j])   
            del_H = H_old-H_new
            if del_H >= 0:
                matrix[i,j] = -matrix[i,j]
            elif del_H < 0:
                    prob = np.exp((del_H)/(temp))   
                    rand = random.random()
                    if rand < prob:
                        matrix[i,j] = -matrix[i,j]
                    else:
                        matrix[i,j] = matrix[i,j]  

        if step >= (sw):

            Ene = EnergyCal(matrix)
            energyarray.append(Ene)


        step =+ 1 


    energy_sum = []
    energy_sum = sum(energyarray)


    plt.figure(10)
    plt.plot(temp, energy_sum, 'ro')


plt.show()
Louise
  • 351
  • 1
  • 6
  • 16

2 Answers2

3

Python for-loops are iterator-based "for-each" loops. The iterating variable is reassigned at the beginning of each iteration. In other words, the following loop:

In [15]: nums = 1,2,5,8

In [16]: for num in nums:
    ...:     print(num)
    ...:
1
2
5
8

Is equivalent to:

In [17]: it = iter(nums)
    ...: while True:
    ...:     try:
    ...:         num = next(it)
    ...:     except StopIteration:
    ...:         break
    ...:     print(num)
    ...:
1
2
5
8

Similarly, the following loops are equivalent:

In [19]: for num in nums:
    ...:     print("num:", num)
    ...:     num += 1
    ...:     print("num + 1:", num)
    ...:
    ...:
num: 1
num + 1: 2
num: 2
num + 1: 3
num: 5
num + 1: 6
num: 8
num + 1: 9

In [20]: it = iter(nums)
    ...: while True:
    ...:     try:
    ...:         num = next(it)
    ...:     except StopIteration:
    ...:         break
    ...:     print("num:", num)
    ...:     num += 1
    ...:     print("num + 1:", num)
    ...:
num: 1
num + 1: 2
num: 2
num + 1: 3
num: 5
num + 1: 6
num: 8
num + 1: 9

Note, C-style for-loops don't exist in Python, but you can always write a while-loop (c-style for loops are essentially syntactic sugar for while-loops):

for(int i = 0; i < n; i++){
    // do stuff
}

Is equivalent to:

i = 0
while i < n:
    # do stuff
    i += 1

Note, the difference is that in this case, iteration depends on i, anything in # do stuff that modifies i will impact iteration, whereas in the former case, iteration depends on the iterator. Note, if we do modify the iterator, then iteration is impacted:

In [25]: it = iter(nums) # give us an iterator
    ...: for num in it:
    ...:     print(num)
    ...:     junk = next(it) # modifying the iterator by taking next value
    ...:
    ...:
1
5
juanpa.arrivillaga
  • 88,713
  • 10
  • 131
  • 172
2

step is overwritten on each iteration based on the original for:

for step in np.arange(0, sw*2):
    step = 1  # doesn't matter, it'll get reset on next iteration
DavidG
  • 24,279
  • 14
  • 89
  • 82
minboost
  • 2,555
  • 1
  • 15
  • 15