求下列题目源代码:【题目描述】:某公司举行职员足球比赛,编写程序用于记录球员的信息(队名、球员姓名、球员号码)和比赛进球数。实现随着比赛的推进,每进一球循环输入进球的队名、球员号码。最后输出该项足球比赛的球员进球情况和金靴奖得主。 输入输出示例如下: 请输入第一支球队队名: "A队" 请输入球队队员数量:9 请输入第1位队员球号、姓名:3 张三 …… 请输入第9位队员球号、姓名:20 王五 请输入第一支球队队名: "B队" 请输入球队队员数量:8 请输入第1位队员球号、姓名:1 梁一 …… 请输入
时间: 2024-03-03 14:50:17 浏览: 176
以下是一个简单的实现,可以记录球员信息,输入进球的队名和球员号码,统计每个球员的进球数,并输出进球情况和金靴奖得主。
```python
class Player:
def __init__(self, team_name, number, name):
self.team_name = team_name
self.number = number
self.name = name
self.goals = 0
def add_goal(self):
self.goals += 1
def __str__(self):
return f"{self.number} {self.name} ({self.team_name}): {self.goals} goals"
class FootballGame:
def __init__(self):
self.teams = {}
self.players = []
def input_team(self):
team_name = input("请输入球队名称: ")
num_players = int(input("请输入球队队员数量: "))
players = []
for i in range(num_players):
number = input(f"请输入第{i+1}位队员球号: ")
name = input(f"请输入第{i+1}位队员姓名: ")
player = Player(team_name, number, name)
players.append(player)
self.players.append(player)
self.teams[team_name] = players
def input_goal(self):
team_name = input("请输入进球队名: ")
number = input("请输入进球球员号码: ")
for player in self.teams[team_name]:
if player.number == number:
player.add_goal()
return
print("球员不存在")
def print_goals(self):
for player in self.players:
print(player)
def get_top_scorer(self):
top_scorer = max(self.players, key=lambda x: x.goals)
return f"{top_scorer.name} ({top_scorer.team_name}) won the Golden Boot with {top_scorer.goals} goals."
```
使用示例:
```python
game = FootballGame()
game.input_team()
game.input_team()
while True:
choice = input("请输入操作:1.输入进球 2.打印进球情况 3.金靴奖得主 4.退出\n")
if choice == "1":
game.input_goal()
elif choice == "2":
game.print_goals()
elif choice == "3":
print(game.get_top_scorer())
elif choice == "4":
break
else:
print("无效操作")
```
阅读全文