简体   繁体   English

Python命令在命令中重复相同的数字(我正在学习python)

[英]Python command repeats same number in command(im learning python)

I tried to make a twitter bot to print random number but it print the same number while testing the command我试图制作一个 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() 

What should I do in order to have random numbers?我应该怎么做才能获得随机数?

Your first block defines the random_number , which is then fixed.您的第一个块定义了random_number ,然后固定。

You should compute the random number in your job你应该在你的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() 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM