【问题标题】:Python command repeats same number in command(im learning python)Python命令在命令中重复相同的数字(我正在学习python)
【发布时间】:2021-06-30 09:38:21
【问题描述】:

我试图制作一个 twitter 机器人来打印随机数,但它在测试命令时会打印相同的数字

import tweepy
import random
import time
import threading
import schedule



start = time.perf_counter()
while time.perf_counter() - start < 2:
    random_number = random.randint(0,10000)
    time.sleep(1.0)



def job():  
    print(random_number)  

# run the function job() every 2 seconds  
schedule.every(2).seconds.do(job)  

while True:  
    schedule.run_pending() 

我应该怎么做才能获得随机数?

【问题讨论】:

    标签: python random numbers


    【解决方案1】:

    您的第一个块定义了random_number,然后将其固定。

    您应该计算job 中的随机数

    import random
    import time
    import threading
    import schedule
    
    
    def job():  
        print(random.randint(0,10000))  
    
    # run the function job() every 2 seconds  
    schedule.every(2).seconds.do(job)  
    
    while True:  
        schedule.run_pending() 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-03-04
      • 2018-03-04
      • 2020-05-22
      • 1970-01-01
      • 2019-03-13
      • 2014-04-20
      • 1970-01-01
      • 2021-04-17
      相关资源
      最近更新 更多