模拟决赛比赛现场得分计算代码python
时间: 2023-11-29 15:02:01 浏览: 47
下面是一个使用Python编写的模拟决赛比赛现场得分计算代码:
```python
import random
def calculate_score(teams):
scores = {}
# 计算每个队伍的得分
for team in teams:
team_score = 0
# 模拟每轮比赛
for round in range(1, 4):
round_score = random.randint(0, 10)
# 根据轮次给予不同的加分权重
if round == 1:
team_score += round_score
elif round == 2:
team_score += 2 * round_score
elif round == 3:
team_score += 3 * round_score
scores[team] = team_score
return scores
# 主程序入口
if __name__ == "__main__":
teams = ["队伍A", "队伍B", "队伍C", "队伍D"]
final_scores = calculate_score(teams)
# 打印每个队伍的得分
for team, score in final_scores.items():
print(f"{team}: 得分 {score}")
```
这段代码利用了random库来生成随机数模拟决赛比赛的得分情况。首先定义了一个函数`calculate_score`来计算每个队伍的得分。在每个队伍的得分计算中,模拟了三轮比赛,每轮比赛的得分都是随机生成的(0到10之间的整数),并根据轮次分别给予不同的加分权重。最后,返回一个包含每个队伍得分的字典。
在主程序入口处,定义了一个包含四个队伍的列表,然后调用`calculate_score`函数来计算得分,并将结果保存在`final_scores`字典中。最后,通过遍历`final_scores`字典,打印每个队伍的得分。