我试图在每次某个值保持不变时将计数器变量更新一次。
主要条件是:
streak = 0
entryno_counter = 1
class OtherDataGet:
streak_type = 3
uod_state = 3
@staticmethod
def uod():
global streak
if entryno_counter == 1:
pass
else:
if values[1] > values_cache[1]: # If value went up
if OtherDataGet.uod_state == 1 or 2: # If it was previously down or same
OtherDataGet.uod_state = 0 # Set state to up
streak = 0 # Reset streak
OtherDataGet.streak_type = 0 # Set streak type to up
elif OtherDataGet.uod_state == 0: # If last state was up
streak += 1 # Add one to streak counter
return 0
elif values[1] < values_cache[1]:
if OtherDataGet.uod_state == 0 or 2:
OtherDataGet.uod_state = 1
streak = 0
OtherDataGet.streak_type = 1
elif OtherDataGet.uod_state == 1:
streak += 1
return 1
elif values[1] == values_cache[1]:
if OtherDataGet.uod_state == 0 or 1:
OtherDataGet.uod_state = 2
streak = 0
OtherDataGet.streak_type = 2
elif OtherDataGet.uod_state == 2:
streak += 1
return 2
正在更新的变量位于全局命名空间中,并且可以随处访问,因此更新它时应该没有问题。
例如,当第一次返回2时,条纹计数器设置为0,第二次返回2,应该设置为1,第三次返回2,它应该是3,等等。
1,125519,0,182701,4,404,的 0,1
2,125519,2,182702,4,404,的 2,1
3,125518,1,182703,4,404,的 1,1
4,125519,0,182704,4,404,的 0,1
5,125519,2,182705,4,404,的 2,1
6,125519,2,182706,4,404,的 2,1
7,125519,2,182706,4,404,的 2,1
8,125519,2,182707,4,404,的 2,1
9,125517,1,182708,4,404,的 1,1
10,125518,0,182709,4,404,的 0,1
11,125517,1,182710,4,404,的 1,1
这是输出数据,您需要查看的是最后两列。倒数第二个是OtherDataGet.uod
返回的值,最后一个值应为streak
。如果你看5-8行,就会有一个2s的条纹,并且该行的最后一个值应分别为1,2,3,4,但即使它应该重置为0,它也会保持为1。 / p>
答案 0 :(得分:0)
当您尝试在函数内部分配全局变量时,它会创建一个局部变量。要防止这种情况发生,请在函数中使用global
关键字。
在这种情况下,那将是:
def uod():
global streak, entryno_counter
其余正常。
答案 1 :(得分:0)
只需在要使用它的任何函数中全局声明变量.EG:
def uod():
global streak