x = 1000
while x <= data['a2goodthousands'] * 1000:
data['MyA2Good'] = x + data['a2goodhundreds']
x = x + 1000
if 1 > data['a2goodthousands']:
data['MyA2Good'] = data['a2goodhundreds']
Above is the code I wrote. It works, but I want to understand exactly why it works. Originally, I wrote it, and it worked, and I moved on. Then I got to thinking why does it update constantly? For more explanation the data is coming from an old PLC that can only store signed 16 bit. Thus, the max number is 32,676. Since the counter will quickly exceed that, every time it hits 1000 we count up by 1 in a 'Thousands' counter, and reset the 'Hundreds'. This puts the actual number I want to view in two different words though. I wrote this code in Ignition to combine the two numbers into 1 spot.
That being said the 'thousands' tag of course starts at 0, so the if statement at the bottom is in place until the first 1000 is counted and it just looks at the hundreds value.
The way I am understanding it is that once my 'Thousands' tag counts up by one, for that moment, I have my while statement condition satisfied by the equal operator (x = 1000 and my tag*1000 = 1000), I add 1000 + my 'Hundreds' counter and store it in the new data tag 'MyA2Good' and then count x up by 1000 to be 2000. That being said, the while statement shouldn't evaluate again until I hit 2000 on my counter now, right? But when monitoring the value stored in my tag, 'MyA2Good' as the hundreds counter increments up 1 at a time, the tag is constantly updated as if the while statement is being constantly evaluated, even though the condition to evaluate is no longer true.
The code is doing what I want it to do, I just seek to understand why exactly.
while the
x
variable is less or equal todata['a2goodthousands'] * 1000:
the while loop is running. Every run of it the value of adata['MyA2Good']
variable is changed tox + ['a2goodhundreds']
and after that assignment the x is ax + 1000
. When thex
will be bigger or equal todata['a2goodthousands'] * 1000
the while loop ends and the if statement is executed. if1
is bigger thandata['a2goodthousands']
thedata['MyA2Good']
variable is equal todata['a2goodhundreds']