先说规则:
一等奖:投注号码与当期开奖号码全部相同(顺序不限,下同),即中奖;
二等奖:投注号码与当期开奖号码中的6个红色球号码相同,即中奖;
三等奖:投注号码与当期开奖号码中的任意5个红色球号码和1个蓝色球号码相同,即中奖;
四等奖:投注号码与当期开奖号码中的任意5个红色球号码相同,或与任意4个红色球号码和1个蓝色球号码相同,即中奖;
五等奖:投注号码与当期开奖号码中的任意4个红色球号码相同,或与任意3个红色球号码和1个蓝色球号码相同,即中奖;
六等奖:投注号码与当期开奖号码中的1个蓝色球号码相同,即中奖。
然后直接上代码:(我的最后一位为蓝区:)
import random
from datetime import datetime
class Lottery:
def __init__(self):
pass
def get_user_guesses(self):
guessed_numbers = []
for i in range(1,7):
while True: # 无限循环,直到满足退出条件
a = int(input(f"请输入第{i}个值,范围是1-33 "))
if 1 <= a <= 33: # 如果输入值在范围内,退出循环
guessed_numbers.append(a)
break
else: # 如果输入值不在范围内,提示用户并继续循环
print("输入值过小或过大")
while True: # 无限循环,直到满足退出条件
b = int(input("请输入第7个值,范围是1-16 "))
if 1 <= b <= 16: # 如果输入值在范围内,退出循环
guessed_numbers.append(b)
break
else: # 如果输入值不在范围内,提示用户并继续循环
print("输入值过小或过大")
print(f"您的号码是{guessed_numbers}")
return guessed_numbers
def produce_winning_numbers(self):
now = datetime.now()
nowtime = now.strftime("%Y-%m-%d %H:%M:%S")
self.winning_numbers = []
for i in range(6):
self.winning_numbers.append(random.randint(1, 34))
self.winning_numbers.append(random.randint(1, 17))
print(f"{nowtime}的中奖号码是{self.winning_numbers}")
return self.winning_numbers
def judge(self, user_guesses, winning_numbers):
count=0
for i in range(0,5):
for j in range(0,5):
if user_guesses[i]==winning_numbers[j]:
count=count+1
count1=0
if user_guesses[-1]==winning_numbers[-1]:
count1=count1+1
if count==6 and count1 ==1:
print("恭喜中了一等奖")
elif count==6:
print("恭喜中了二等奖")
elif count==5 and count1==1:
print("恭喜中了三等奖")
elif count==4 and count1 ==1:
print("恭喜中了四等奖")
elif count==3 and count1==1:
print("恭喜中了五等奖")
elif count1==1:
print("恭喜中了六等奖")
else :
print("抱歉,没有中奖")
def play(self):
user_guesses = self.get_user_guesses()
self.produce_winning_numbers()
self.judge(user_guesses, self.winning_numbers)
test = Lottery()
test.play()
然后结果展示,有中一等将的小伙伴建议买彩票,因为你真的中彩票了。