Home > database >  How do I run a conditional statement "only once" and every time it changes?
How do I run a conditional statement "only once" and every time it changes?

Time:12-26

I might be asking a simple question. I have a python program that runs every minute. But I would like a block of code to only run once the condition changes? My code looks like this:

# def shortIndicator():
a = int(indicate_5min.value5)
b = int(indicate_10min.value10)
c = int(indicate_15min.value15)
if a   b   c == 3:
  print("Trade posible!")
else:
  print("Trade NOT posible!")

# This lets the processor work more than it should.
"""run_once = 0  # This lets the processor work more than it should.
while 1:
    if run_once == 0:
        shortIndicator()
        run_once = 1"""

I've run it without using a function. But then I get an output every minute. I've tried to run it as a function, when I enable the commented code it sort of runs, but also the processing usage is more. If there perhaps a smarter way of doing this?

CodePudding user response:

If I understand it correctly, you can save previous output to a file, then read it at the beginning of program and print output only if previous output was different.

CodePudding user response:

previous = None    
def shortIndicator():
    a = int(indicate_5min.value5)
    b = int(indicate_10min.value10)
    c = int(indicate_15min.value15)
    if a   b   c !=previous:
        if a   b   c == 3:
            print("Trade posible!")
        else:
            print("Trade NOT posible!")
    previous = a   b   c
  • Related