js
import random
class DoubleColorBall:
def __init__(self):
self.balance = 0
self.red_winning_numbers = []
self.blue_winning_number = None
def recharge(self, amount):
"""充值"""
if amount > 0:
self.balance += amount
print(f"充值成功,充值金额:{amount}元,当前余额:{self.balance}元")
else:
print("充值金额必须大于0元")
def generate_winning_numbers(self):
"""生成中奖号码"""
self.red_winning_numbers = random.sample(range(1, 34), 6)
self.red_winning_numbers.sort()
self.blue_winning_number = random.choice(range(1, 17))
print(f"本期中奖号码为:红球{self.red_winning_numbers}, 蓝球{self.blue_winning_number}")
def check_winning(self, user_red_balls, user_blue_ball):
"""检测用户是否中奖"""
red_hit = set(user_red_balls) & set(self.red_winning_numbers)
blue_hit = user_blue_ball == self.blue_winning_number
red_count = len(red_hit)
if red_count == 6 and blue_hit:
print("恭喜你,中了一等奖!(6+1)")
elif red_count == 6:
print("恭喜你,中了二等奖!(6+0)")
elif red_count == 5 and blue_hit:
print("恭喜你,中了三等奖!(5+1),单注奖金固定为3000元")
elif red_count == 5 or (red_count == 4 and blue_hit):
print("恭喜你,中了四等奖!(5+0或4+1),单注奖金固定为200元")
elif red_count == 4 or (red_count == 3 and blue_hit):
print("恭喜你,中了五等奖!(4+0或3+1),单注奖金固定为10元")
elif blue_hit:
print("恭喜你,中了六等奖!(2+1或1+1或0+1),单注奖金固定为5元")
else:
print("很遗憾,你没有中奖。")
def buy_ticket(self):
"""购买双色球(每注2元)"""
try:
num_tickets = int(input("请输入购买注数:"))
except ValueError:
print("请输入有效的数字。")
return
total_cost = 2 * num_tickets
if self.balance >= total_cost:
self.balance -= total_cost
tickets = []
for i in range(num_tickets):
print(f"\n购买第{i + 1}注:")
print("请选择彩票号码:")
choice = input("输入1表示自动选择,输入2表示手动选择: ")
if choice == '1':
red_balls = random.sample(range(1, 34), 6)
blue_ball = random.choice(range(1, 17))
elif choice == '2':
red_balls = []
print("请选择红球号码(1-33,不重复):")
while len(red_balls) < 6:
try:
num = int(input(f"请输入第{len(red_balls) + 1}个红球号码:"))
if num < 1 or num > 33 or num in red_balls:
print("数字无效或重复,请重新输入。")
else:
red_balls.append(num)
except ValueError:
print("请输入有效的数字。")
print("请选择蓝球号码(1-16):")
while True:
try:
blue_ball = int(input("请输入蓝球号码:"))
if blue_ball < 1 or blue_ball > 16:
print("数字无效,请重新输入。")
else:
break
except ValueError:
print("请输入有效的数字。")
else:
print("无效的选择,默认自动选择号码。")
red_balls = random.sample(range(1, 34), 6)
blue_ball = random.choice(range(1, 17))
red_balls.sort() # 红球排序
print(f"购买成功!您的双色球号码为:红球{red_balls}, 蓝球{blue_ball}")
tickets.append((red_balls, blue_ball))
print(f"购买后余额:{self.balance}元")
# 生成开奖号码
self.generate_winning_numbers()
# 检测是否中奖
for i in range(num_tickets):
red_balls, blue_ball = tickets[i]
print(f"\n检查第{i + 1}注:")
self.check_winning(red_balls, blue_ball)
else:
print(f"余额不足,无法购买!当前余额:{self.balance}元")
def check_balance(self):
"""查询余额"""
print(f"当前余额:{self.balance}元")
def exit_program(self):
"""退出程序"""
print("感谢使用,再见!")
def main():
dcball = DoubleColorBall()
while True:
print("\n欢迎使用双色球模拟器")
print("1. 充值")
print("2. 购买双色球")
print("3. 查询余额")
print("4. 退出")
choice = input("请输入您的选择:")
if choice == '1':
try:
amount = float(input("请输入充值金额:"))
except ValueError:
print("请输入有效的数字。")
continue
dcball.recharge(amount)
elif choice == '2':
if dcball.balance < 2:
print("余额不足,请先充值")
else:
dcball.buy_ticket()
elif choice == '3':
dcball.check_balance()
elif choice == '4':
dcball.exit_program()
break
else:
print("无效的选择,请重新输入!")
if __name__ == "__main__":
main()
本文作者:wzq
本文链接:
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!