HelpMeCode
HelpMeCode

Reputation: 23

Python Condition Based on Time

I don't even know how to go about writing this, but will give it a shot. Don't beat me up too badly :-D

I'm using Python3 and want to write a condition based on time.

If current price is below a certain price for 1 minute, I want to sale.

if num1 < num2:
   print("NUM1 has been LESS THAN NUM2 for 1 minute")

I'm stuck on including the time portion for this.

Upvotes: 1

Views: 660

Answers (2)

Kabilan Mohanraj
Kabilan Mohanraj

Reputation: 1916

This solution is drawn from this Stack Overflow thread. The answer was given by Nithish Albin

Here, you use the time import. When your if condition results to true, the print statement is executed followed by the while loop which waits for a minute(does nothing). The condition for while loop to terminate is that the current time should be less than i (60 seconds in the future).

import time

num1 = 1
num2 = 2
i=int(time.time())+60  # setting a checkpoint 60s in the future
if num1 < num2:
    print("NUM1 has been LESS THAN NUM2 for 1 minute")
    while(int(time.time())<=i): # checking if the current time is past the checkpoint "i"
        pass
    

Upvotes: 1

Pavan Suvarna
Pavan Suvarna

Reputation: 501

One simple way to achieve this using while loop

import time

condition = True

num1 = 10
num2 = 11
t = time.time()

while condition == True:
    t1 = time.time()
    if num1 < num2:
        if t1 - t > 60:
            print("NUM1 has been LESS THAN NUM2 for 1 minute")
            condition = False

            #print(t1-t)

Upvotes: 0

Related Questions