只是练习,刚刚开始学习,因为经验尚浅,缺少优化,将自己写过的东西先记录下来,等将来回头看看。对上次的面向过程试着改为面向对象,进行了一次升级。
版本1链接:https://blog.csdn.net/Zohn_Sun/article/details/122407595
双色球
“双色球”就是从33个红色球号码中选出6个号码,再从16个蓝色球号码中选出1个号码,组成乐透式投注。双色球中奖条件:
一等奖(6+1),即中红球6个,篮球1个;
二等奖(6+0),即中红球6个;
三等奖(5+1),即中红球5个,篮球1个;
四等奖(5+0、4+1),即中红球4个,篮球1个或者中红球5个;
五等奖(4+0、3+1),即中红球3个,篮球1个或者中红球4个;
六等奖(2+1、1+1、0+1),即中红球2个,篮球1个或者中红球1个,篮球1个或者中篮球1个
————————————————
import random
class LotteryModel: # Model 建立彩票模型
def __init__(self, list_red_ball=list, blue_ball=0):
self.red_ball = list_red_ball
self.blue_ball = blue_ball
class LotteryView:
def __init__(self):
self.controller = LotteryController()
def __start(self):
print("从33个红色球号码中选出6个号码,再从16个蓝色球号码中选出1个号码,组成乐透式投注。\n"
"双色球中奖条件:\n"
"一等奖(6+1),即中红球6个,篮球1个;\n"
"二等奖(6+0),即中红球6个;\n"
"三等奖(5+1),即中红球5个,篮球1个;\n"
"四等奖(5+0、4+1),即中红球4个,篮球1个或者中红球5个;\n"
"五等奖(4+0、3+1),即中红球3个,篮球1个或者中红球4个;\n"
"六等奖(2+1、1+1、0+1),即中红球2个,篮球1个或者中红球1个,篮球1个或者中篮球1个\n")
def __select_ball(self):
"""
手动输入彩票号码
"""
list_red_ball = [] # 建立临时红球列表
blue_ball = 0 # 建立临时蓝球
ball = 0 # 创建临时参数用以指向号码
while len(list_red_ball) < 6: # 建立红球输入循环规则
try:
ball = int(input(f"目前您已投注{len(list_red_ball)}个数字,请录入你投注的数字(1-33):"))
if ball < 1 or ball > 33: # 建立红球输入录入规则
print("您投注的数字超出范围,请重新投注")
continue
if ball not in list_red_ball:
list_red_ball.append(ball)
else:
print("您投注的数字已重复,请重新投注")
except:
print("输入内容不符合")
continue
while blue_ball == 0: # 建立蓝球输入录入规则
try:
ball = int(input("前您已投注6个数字,请录入一个您要投注的蓝色球数字(1-16):"))
if ball > 16 or ball < 1:
print("您投注的数字超出范围,请重新投注")
else:
blue_ball = ball
except:
print("输入内容不符合")
continue
model = LotteryModel() # 引入模型类进行打包
model.red_ball = list_red_ball
model.blue_ball = blue_ball
self.controller.add_ball(model) # 将打包好的数据送入controller类
a = " ".join(str(e) for e in list_red_ball)
print("您投注的号码为:红色球:", a, "蓝色球:", str(blue_ball))
def enter_execs(self): # 程序执行入口
while True:
view.__start()
view.__select_ball()
view.__select_count()
def __select_count(self): # 设置抽奖次数
self.item = int(input("请输入您要参与的抽奖次数,最高1000次,输入超过1000则为1000:"))
self.__display_result(self.controller.redeem_count(self.item))
@property # 对抽奖次数附以属性,以限制次数
def item(self):
return self.__item
@item.setter
def item(self, value):
if value > 100:
self.__item = 1000
elif value < 0:
self.__item = 1
def __display_result(self, count): # 显示抽奖结果
print(
f"本轮抽奖{self.item}次,\n您抽中:\n一等奖{count[1]}次,\n二等奖{count[2]}次,\n三等奖{count[3]}次,\n四等奖{count[4]}次,\n五等奖{count[5]}次,\n六等奖{count[6]}次,\n没中奖{count[0]}次.")
class LotteryController: # 建立controller类,负责运算及存储
def __init__(self): # 建立对象,用以存储指向来自Model打包好的彩票数据
self.__list_lottery_select = []
self.__list_lottery_rand = []
# self.executions=1
def add_ball(self, model): # 加入自选彩票结果
self.__list_lottery_select.clear()
self.__list_lottery_select.append(model)
# self.executions = 1
def __add_ball_rand(self, model): # 导入彩票摇号结果
self.__list_lottery_rand.clear()
self.__list_lottery_rand.append(model)
def __random_data(self): # 机器摇号
model = LotteryModel() # 调用MODEL模块对红球及蓝球打包
model.red_ball = random.sample([i for i in range(1, 34)], k=6)
model.blue_ball = random.randint(1, 16)
self.__add_ball_rand(model)
# print("已抽奖",self.executions,"次")
# self.executions+=1
def __redeem(self): # 兑奖 将蓝色与红色结果进行比对
if self.__redeem_blue():
if self.__redeem_red() == 6:
return 1
elif self.__redeem_red() == 5:
return 3
elif self.__redeem_red() == 4:
return 4
elif self.__redeem_red() == 3:
return 5
else:
return 6
else:
if self.__redeem_red() == 6:
return 2
elif self.__redeem_red() == 5:
return 4
elif self.__redeem_red() == 4:
return 5
else:
return 0
def __redeem_blue(self): # 兑奖-蓝色
if self.__list_lottery_rand[0].blue_ball == self.__list_lottery_select[0].blue_ball:
return True
return False
def __redeem_red(self): # 兑奖-红色
selsect_red = set(self.__list_lottery_select[0].red_ball)
rand_red = set(self.__list_lottery_rand[0].red_ball)
num = selsect_red.intersection(rand_red)
return len(num)
def redeem_count(self, count): # 进行规定次数的抽奖循环
self.dic_result = {i:0 for i in range(7)}
for i in range(count):
self.__random_data()
num = self.__redeem()
self.dic_result[num] += 1
return self.dic_result
if __name__ == '__main__':
view = LotteryView()
view.enter_execs()