[Solved] python while loop iteration [closed]


Your while loop is never executing, because

while absolute_avg > calculated_std:
    ...

is never satisfied. In fact absolute_avg == calculated_std.

There seems to be no reason you cannot instantiate absolute_avg and calculated_std to be values such that they will succeed on the first pass of the while loop.

Say:

calculated_std = 0.0
absolute_avg = 1.0

Maybe being a cleaner solution, you could implement a do-while-esque loop, to ensure that the loop is ran at least once, like this:

test_energy = []
calculated_std = float('inf')
absolute_avg = float('inf')
x = 1
while True:
 for i in range(x,x+10):
    energy = system.energy().value()
    print(energy)
    test_energy.append(energy)
    std = AverageAndStddev()
    A = sum(test_energy[-10:-5])/5
    B = sum(test_energy[-5:])/5
    absolute_avg = abs(A - B)
    std.accumulate(A)
    std.accumulate(B)
    avg = std.average()
    calculated_std = std.standardError()
    x = x+1
  if absolute_avg > calculated_std:
    break

4

solved python while loop iteration [closed]