本帖最后由 single_cat 于 2022-3-9 10:16 编辑
新人,刚刚接触编程,写得不好,很多东西都还不熟,请大家多指教
另外,有个问题请教一下,用什么方法能多线程运行?
目前这个这个单线程运行太慢,i5-540m最长要一个多小时,最后算出来要花接近6亿RMB
先谢谢各位大佬{:1_893:}
[Python] 纯文本查看 复制代码
import random
import time
def roll_red_ticket():
"""随机生成红球号码"""
redball_list = []
while len(redball_list) < 6:
redball_number = random.randint(1, 33)
redball_list.append(redball_number)
redball_list = list(set(redball_list))
redball_list.sort()
return redball_list
def roll_blue_ticket():
"""随机生成蓝色球"""
blueball_list = []
while len(blueball_list) < 2:
blueball_number = random.randint(1, 16)
blueball_list.append(blueball_number)
blueball_list = list(set(blueball_list))
return blueball_list
# 运行开始时间
print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))
count = 0
my_redball_list = roll_red_ticket()
my_blueball_list = roll_blue_ticket()
win_redball_list = roll_red_ticket()
win_blueball_list = roll_blue_ticket()
print(f"此次开奖号码:\n\t红球:{win_redball_list}\n\t蓝球:{win_blueball_list}")
while True:
if my_redball_list != win_redball_list or my_blueball_list != win_blueball_list:
my_redball_list = roll_red_ticket()
my_blueball_list = roll_blue_ticket()
count += 1
else:
print(f"你购买的号码:\n\t红球:{my_redball_list}\n\t蓝球:{my_blueball_list}")
print(f"恭喜你,中了大奖!!!")
print(f"总计需要购买{count}注彩票,花费{count * 2}元。")
#运行结束时间
print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))
break |