编辑
2024-07-26
test
00
请注意,本文编写于 251 天前,最后修改于 251 天前,其中某些信息可能已经过时。
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 if red_hit and blue_hit: print("恭喜你,中了一等奖!") elif red_hit: print(f"你中了{len(red_hit)}个红球!") elif blue_hit: print("你中了蓝球!") else: print("很遗憾,你没有中奖。") def buy_ticket(self, cost=2): """购买双色球(默认每注2元)""" if self.balance >= cost: self.balance -= cost 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("请选择6个红球号码(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个蓝球号码(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}") print(f"购买后余额:{self.balance}元") # 生成开奖号码 self.generate_winning_numbers() # 检测是否中奖 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': amount = float(input("请输入充值金额:")) 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 许可协议。转载请注明出处!