用random实现一个双色球系统,包括购买彩票,查询今日开奖和查询往期结果
时间: 2024-03-03 22:52:02 浏览: 18
好的,以下是用Python实现的双色球系统:
```python
import random
class DoubleColorBall:
def __init__(self):
self.red_balls = []
self.blue_balls = []
self.past_results = []
def generate_balls(self):
# 生成红球
while len(self.red_balls) < 6:
ball = random.randint(1, 33)
if ball not in self.red_balls:
self.red_balls.append(ball)
# 生成蓝球
self.blue_balls.append(random.randint(1, 16))
def buy_ticket(self):
self.generate_balls()
print("您购买的双色球号码为:")
print("红球:", self.red_balls)
print("蓝球:", self.blue_balls)
def check_today_result(self):
self.generate_balls()
print("今日开奖结果为:")
print("红球:", self.red_balls)
print("蓝球:", self.blue_balls)
self.past_results.append((self.red_balls, self.blue_balls))
def check_past_result(self):
if len(self.past_results) == 0:
print("暂无历史开奖结果")
else:
for i, result in enumerate(self.past_results):
print(f"第{i+1}期开奖结果为:")
print("红球:", result[0])
print("蓝球:", result[1])
# 示例运行
dc_ball = DoubleColorBall()
dc_ball.buy_ticket()
dc_ball.check_today_result()
dc_ball.check_past_result()
```
运行结果会输出您购买的号码、今日开奖结果和历史开奖结果。您可以通过调用 `buy_ticket()` 方法来购买双色球彩票,通过调用 `check_today_result()` 方法来查询今日开奖结果,通过调用 `check_past_result()` 方法来查询历史开奖结果。