我必须每秒运行代码并比较当前执行的返回值是否相等以及之前的返回值。我试过线程,但我找不到存储值进行比较的方法。
import numpy as np
import random
def ran ():
a = random.randint(1,101)
return a
答案 0 :(得分:1)
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")
答案 1 :(得分:0)
这个怎么样:
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)