Python篮球比赛计分程序
时间: 2024-04-16 08:22:23 浏览: 185
python 实现比赛对手
Python篮球比赛计分程序可以用来记录和计算篮球比赛的得分情况。以下是一个简单的示例程序:
```python
class BasketballGame:
def __init__(self, team1, team2):
self.team1 = team1
self.team2 = team2
self.team1_score = 0
self.team2_score = 0
def score(self, team, points):
if team == self.team1:
self.team1_score += points
elif team == self.team2:
self.team2_score += points
else:
print("无效的队伍")
def get_score(self):
return f"{self.team1}: {self.team1_score} - {self.team2}: {self.team2_score}"
# 示例用法
game = BasketballGame("湖人", "勇士")
game.score("湖人", 2) # 湖人队得2分
game.score("勇士", 3) # 勇士队得3分
print(game.get_score()) # 输出当前比分
```
这个程序定义了一个`BasketballGame`类,通过`score`方法可以为指定队伍增加得分,通过`get_score`方法可以获取当前比分。你可以根据实际需求进行扩展和修改。
阅读全文