import random import threading from time import sleep import keyboard def read_phone(): with open('pool.txt', mode='r') as f: phones = [int(p) for p in f.readlines()] return phones def print_phone(phones): global lucy_p max = len(phones) while start_flag: i = random.randint(0, max - 1) lucy_p = phones[i] print(lucy_p) sleep(1) # 开启一个线程接收控制台的输入 输入结束之后抽奖结束 def stop(): global start_flag start_flag = True print("开始抽奖......\n") while start_flag: if keyboard.is_pressed('enter'): # if key 'enter' is pressed sleep(random.random()) # 模拟按停止键后号码慢慢停下来 start_flag = False sleep(1) if __name__ == '__main__': phones = read_phone() # 读取文件中所有的电话号码 threadStop = threading.Thread(target=stop) # 先去初始化start_flag threadStop.start() threadStart = threading.Thread(target=print_phone, args=(phones,)) threadStart.start() threadStart.join() threadStop.join() print("幸运号码是:", lucy_p)
python 实现模拟抽奖
最新推荐文章于 2024-04-11 16:12:10 发布