0
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.

Alex
  • 1
  • 1

1 Answers1

1

while the x variable is less or equal to data['a2goodthousands'] * 1000: the while loop is running. Every run of it the value of a data['MyA2Good'] variable is changed to x + ['a2goodhundreds'] and after that assignment the x is a x + 1000. When the x will be bigger or equal to data['a2goodthousands'] * 1000 the while loop ends and the if statement is executed. if 1 is bigger than data['a2goodthousands'] the data['MyA2Good'] variable is equal to data['a2goodhundreds']

Aleksander Ikleiw
  • 2,549
  • 1
  • 8
  • 26
  • So after the first iteration of adding x + 'a2goodhundreds' , should not the x = x + 1000 occur, and at that point x = 2000, which means x is no longer <= 'a2goodthousands'*1000 because that value is only 1000 until the hundreds counter reaches 1000 again. Whats confusing me is it seems to me the while loop should break after doing the two addition steps and not add again until 2000 is reached on the counter? – Alex Jul 13 '20 at 11:26
  • @Alex you do not understand. you are adding `x + a2goodhundreds` not to the `x` variable, but making it as a value which you assign to the `data['MyA2Good']` – Aleksander Ikleiw Jul 13 '20 at 11:28
  • I understand that, but is not the x = x +1000 adding to the x variable? I mean I thought it was, but it must not be to keep the loop running? If i comment out that line it breaks the code and nothing happens, so maybe that is the line I am confused about? Is there some interaction to the x = 1000 outside the while condition, and the x = x + 1000 inside that I do not understand? – Alex Jul 13 '20 at 11:33
  • the `x = x +1000` lines adds `1000` to x whenever the 1 lap occured. In simpler code you can write `x += 1000` – Aleksander Ikleiw Jul 13 '20 at 11:34
  • Can you explain what 'the 1 lap occurred' means? Thanks a lot for the responses by the way I appreciate it! – Alex Jul 13 '20 at 11:36
  • @Alex if you appreciate it can you upvote it and accept it? :)) While loop is a loop that does laps until the condition returns the `False`. So it is basically the for loop that runs until the condition returns `False` – Aleksander Ikleiw Jul 13 '20 at 11:37
  • Yes, I absolutely will thank you. So my lapse in understanding is this: I thought the x += 1000 line was PART of the while loop, however it is technically 'outside' of it so to speak, and does not run until the while loop becomes false! Which will not occur until the thousands tag becomes 2, then loop breaks, x = 2000 now, and loop becomes true again? – Alex Jul 13 '20 at 11:40
  • Also, I am too new to this forum apparently for it to display my upvote, it says my reputation is under 15 and it will record it however not display. – Alex Jul 13 '20 at 11:41
  • No, no. The `x += 1000` is the part of a `while loop`. The purpose of the `while loop ` is to run the same code over and over until the condition is `False`. Every time the code got executed, the condition is checked right after the `x += 1000` – Aleksander Ikleiw Jul 13 '20 at 11:42
  • Okay... that is what I thought originally... so x starts at 1000, once my condition becomes true it adds x to my 'hundreds' and stores it into 'MyA2Good'. Then it immediately adds 1000 to x making x now 2000 correct? – Alex Jul 13 '20 at 11:48
  • @Alex it adds `1000` to `x` until the condition in while loop returns `False` – Aleksander Ikleiw Jul 13 '20 at 11:49
  • Once it adds 1000 to x, x becomes 2000 right? Then my While loop should become false because the data['a2thousands'] * 1000 expression it is being evaluated against stays at 1000 for quite some time. The encoder incrementing the counter in the PLC takes about 15-20 minutes to count up to the next 1000. – Alex Jul 13 '20 at 11:51
  • But during that time it is waiting to become True again, the tag 'MyA2Good' that is = to x + 'a2goodhundreds' is being updated everytime 'a2goodhundreds' counts up by 1 – Alex Jul 13 '20 at 11:58
  • Maybe I can explain it better..... The while loop condition is only true momentarily, the second the x += 1000 occcurs, the footage counter needs to count up by another 1000 before the loop condition becomes True again.... that being said, the value in data['mya2good'] is constantly incrementing up +1 as the footage counter 'hundreds' value goes up +1 at a time. --- I am wondering if it is a function of ignition platform I am using. – Alex Jul 13 '20 at 16:42