简体   繁体   English

Python 脚本问题,可能很简单,但我对这段代码没有什么经验

[英]Python Scripting question, probably very simple but I have little experience in this code

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.有关更多解释,数据来自只能存储带符号的 16 位的旧 PLC。 Thus, the max number is 32,676.因此,最大数量为 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'.由于计数器会很快超过该值,因此每次达到 1000 时,我们都会在“千”计数器中加 1,并重置“百”。 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.我在 Ignition 中编写了这段代码,将这两个数字合并到一个位置。

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.话虽这么说,“千”标签当然从 0 开始,所以底部的 if 语句一直存在,直到第一个 1000 被计算在内,它只查看数百个值。

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?我理解它的方式是,一旦我的“千”标签加一,那一刻,我的 while 语句条件被等号运算符(x = 1000 和我的标签 * 1000 = 1000)满足,我加 1000 + 我的“Hundreds”计数器并将其存储在新数据标签“MyA2Good”中,然后将 x 加 1000 为 2000。话虽如此,直到我现在在计数器上达到 2000 之前,while 语句不应该再次评估,对吧? 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.但是,当监视存储在我的标签中的值时,“MyA2Good”随着数百个计数器一次递增 1,标签会不断更新,就好像 while 语句正在不断评估一样,即使评估的条件不再为真。

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 to data['a2goodthousands'] * 1000: the while loop is running. while x变量小于或等于data['a2goodthousands'] * 1000: while 循环正在运行。 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 .每次运行时, data['MyA2Good']变量的值都会更改为x + ['a2goodhundreds'] ,并且在分配之后 x 是x + 1000 When the x will be bigger or equal to data['a2goodthousands'] * 1000 the while loop ends and the if statement is executed.x大于或等于data['a2goodthousands'] * 1000时,while 循环结束并执行 if 语句。 if 1 is bigger than data['a2goodthousands'] the data['MyA2Good'] variable is equal to data['a2goodhundreds']如果1大于data['a2goodthousands'] ,则data['MyA2Good']变量等于data['a2goodhundreds']

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM