0

I'm trying to update a counter variable by one each time a certain value stays the same.

The main conditional is this:

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

The variable that is being updated is in the global namespace, and is accessible everywhere so there should be no problem in updating it.

For example, when a 2 is returned the first time, the streak counter is set to 0, the second time a 2 is returned, it should be set to 1, the third time a 2 is returned, it should be a 3, etc.

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

This is the output data, all you need to look at is the last two columns. The second to last is the returned value from OtherDataGet.uod, and the last one should be streak. If you look at lines 5-8, there is a streak of 2s, and the last value on the line should be 1, 2, 3, 4 respectively, but it it's staying as 1 even when it should be reset to 0.

Ari Madian
  • 357
  • 1
  • 4
  • 13
  • Possible duplicate of [Using global variables in a function other than the one that created them](https://stackoverflow.com/questions/423379/using-global-variables-in-a-function-other-than-the-one-that-created-them) – Christian Dean May 26 '17 at 01:51

2 Answers2

0

When you try to assign to a global variable inside a function, it creates a local variable instead. To prevent this from happening, use the global keyword inside your function.

In this case, that would be:

def uod():
    global streak, entryno_counter 

And the rest as normal.

  • My bad, there was a mistake in how the code was copied, it is declared as global at the top of the function, and still doesn't work. – Ari Madian May 26 '17 at 02:17
0

Just declare the variable globally inside of any function you want to use it in. EG:

def uod():
    global streak
Benjamin Commet
  • 350
  • 3
  • 8