Doomski
Doomski

Reputation: 117

Running a function every second and compare with a value a second before.

I have to run the code every second and compare if the return values are equal or not for the current execution and a second before. I tried threading but i couldn't find a way to store the values to compare.

import numpy as np
import random 

def ran ():
    a = random.randint(1,101)
    return a    

Upvotes: 0

Views: 159

Answers (2)

sobek
sobek

Reputation: 1426

What about this:

import random
import time


def ran():
    a = random.randint(1, 101)
    return a


previous_rand = None
while True:
    new_rand = ran()
    if previous_rand and previous_rand == new_rand:
        print 'Equal!'
    else:
        print 'Not equal!'
    previous_rand = new_rand
    time.sleep(1)

Upvotes: 0

BigZee
BigZee

Reputation: 496

ok i wrote some similar code a few years ago i hope it helps

import numpy as np
import random
import time


def ran ():
    a = random.randint(1,101)
    print(a)
    return a


def chk(a,b):
    if a==b:
        return True
    else:
        return False

while True:
    x=ran()
    time.sleep(1)
    x3=ran()
    s=chk(x,x3)
    if s==True:
        print("Both numbers are same")
    else:
        print("Not Equall")

Upvotes: 1

Related Questions