前言:不知道有没有人和我一样,学习python编程的时候有个想法,模似中奖程序,预测中奖号码。废话不多说,直接实操。
一、红球1-32 蓝球1-15
二、中奖规则
一等奖:6个红色球号码+1个蓝色球号码全中。(6+1)
red == 6 and blue ==1
二等奖:6个红色球号码全中。(6+0)
red == 6
三等奖:5个红色球号码+1个蓝色球号码。(5+1)
red == 5 and blue == 1
四等奖:5个红色球号码或者4个红色球号码+1个蓝色球号码
red + blue == 5
五等奖:4个红色球号码或者3个红色球号码+1个蓝色球号码
red + blue == 4
六等奖:2个红色球号码+1个蓝色球号码或者1个红色球号码+1个蓝色球号码或者只有1个蓝
(red == 2 and blue == 1) or (red ==1 and blue ==1) or blue == 1
三、random模块使用
nums = []
for i in range(6):
while True:
num = randint(1, 32)
if num not in nums:
nums.append(num)
break
blue = randint(1, 15)
nums = sorted(nums)
nums.append(blue)
四、1、随机生成6个红球,随机生成1个蓝球。
2、根据规则判断中几等奖
from random import randint
def create_num():
nums = []
for i in range(6):
while True:
num = randint(1, 32)
if num not in nums:
nums.append(num)
break
blue = randint(1, 15)
nums = sorted(nums)
nums.append(blue)
return nums
def pan_duan(nums, buy_num):
red = 0
blue = 0
for i in buy_num[:-1]: # 判断中了多少个
if i in nums[:-1]:
red += 1
if nums[-1] == buy_num[-1]: # 判断蓝球有没有中
blue +=1
if red == 6 and blue ==1:
return 1
elif red == 6:
return 2
elif red == 5 and blue == 1:
return 3
elif red + blue == 5:
return 4
elif red + blue == 4:
return 5
elif (red == 2 and blue == 1) or (red ==1 and blue ==1) or blue == 1:
return 6
else:
return 0
nums = create_num()
buy_num = [3, 7, 12, 22, 23, 25, 29]
print(f"您中了:{pan_duan(nums, buy_num)} 等奖" )
sum_count =0
for i in range(10000):
nums = create_num()
# print(nums)
count = pan_duan(nums, buy_num)
if count != 0:
print(f"第{i}次中{count}等奖")
sum_count +=1
print(f"共中奖:{sum_count}次")