import random
class Lottery:
def __init__(self):
# 用来存储彩票的号码
self.number_list = []
# 彩票价格
self.price = 2
def show(self):
self.number_list.sort()
print(f'{self.number_list}')
class System:
def __init__(self):
pass
# 得到一组包含随机号码的彩票对象
def creat_lottey(self):
# 创建一个彩票对象
lottery = Lottery()
# 循环生成随机号码
for i in range(3):
n = random.randint(0,9)
# 将生成的号码存储到彩票对象的列表属性中
lottery.number_list.append(n)
else:
lottery.number_list.sort()
return lottery
# 系统开奖
def run(self):
return self.creat_lottey()
class Person:
def __init__(self,name,money):
self.name = name
self.money = money
# 存储所有购买的彩票
self.lottey_list = []
def buy(self):
c = input('1.手选 2.机选')
if c == '1':
self.buy_you()
elif c == '2':
self.buy_cmp()
# 得到一个彩票对象,存储列表中
def buy_you(self):
if self.money >= 2:
# 购买的彩票对象
lottery = Lottery()
for i in range(1, 4):
n = eval(input(f'请输入第{i}个号码'))
lottery.number_list.append(n)
else:
lottery.number_list.sort()
# 将购买的彩票对象存储到列表中
self.lottey_list.append(lottery)
print(f'{self.name}手选号码:{lottery.number_list} 消费:{lottery.price}元钱')
# 钱数减少
self.money -= lottery.price
# 展示剩余钱数
self.show_left_money()
else:
print('剩余金额不足')
# 得到一个彩票对象,存储到列表中
def buy_cmp(self):
count = eval(input('请输入你要购买的注数:'))
for i in range(count):
if self.money >= 2:
lottery = System().creat_lottey()
# 将购买的彩票存储到彩票列表中
self.lottey_list.append(lottery)
print(f'{self.name}机选号码:{lottery.number_list} 消费:{lottery.price}钱')
# 钱数减少
self.money -= lottery.price
# 展示剩余钱数
self.show_left_money()
else:
print('剩余金额不足')
# 查看开奖(拿自己购买的号码,与系统开奖的结果进行比对)
def check(self,lottery_run:Lottery):
print(f'开奖号码为:{lottery_run.number_list}')
for lottery in self.lottey_list:
if lottery.number_list == lottery_run.number_list:
print(f' {lottery.number_list}中出200, 金钱增加200')
self.money += 200
else:
print(f'{lottery.number_list} 与中奖擦肩而过!')
else:
self.show_left_money()
# 展示剩余钱数
def show_left_money(self):
print(f'{self.name}还剩余金钱{self.money}')
p1 = Person('铁锤',1000000)
p1.buy()
# 创建系统对象
s = System()
# 系统开奖
lottery_run = s.run()
#人去兑奖
p1.check(lottery_run)
来源:CSDN
作者:忌辰
链接:https://blog.csdn.net/weixin_44797793/article/details/103525819