python案例统计红黄牌数量哪队更多?

wen python案例 2

本文目录导读:

python案例统计红黄牌数量哪队更多?

  1. 版本1:基础版(适合学习)
  2. 版本2:只比较总牌数(不考虑红黄牌权重)
  3. 版本3:完整版(包含用户输入)
  4. 版本4:面向对象版(更专业)
  5. 运行效果示例
  6. 使用建议

我来帮您设计一个统计红黄牌数量的Python程序,这里提供几个不同版本的实现:

版本1:基础版(适合学习)

def count_cards(team1_cards, team2_cards):
    """
    统计两队红黄牌数量并比较
    team1_cards, team2_cards: 字典,包含'red'和'yellow'的键
    """
    # 计算各队总分(红牌3分,黄牌1分)
    team1_score = team1_cards['red'] * 3 + team1_cards['yellow'] * 1
    team2_score = team2_cards['red'] * 3 + team2_cards['yellow'] * 1
    print(f"队1: 红牌 {team1_cards['red']} 张,黄牌 {team1_cards['yellow']} 张,总分 {team1_score}")
    print(f"队2: 红牌 {team2_cards['red']} 张,黄牌 {team2_cards['yellow']} 张,总分 {team2_score}")
    if team1_score > team2_score:
        return "队1 红黄牌更多(按3分制计算)"
    elif team2_score > team1_score:
        return "队2 红黄牌更多(按3分制计算)"
    else:
        return "两队红黄牌数量相同"
# 示例数据
team1 = {'red': 2, 'yellow': 3}  # 2张红牌,3张黄牌
team2 = {'red': 1, 'yellow': 5}  # 1张红牌,5张黄牌
result = count_cards(team1, team2)
print(result)

版本2:只比较总牌数(不考虑红黄牌权重)

def compare_total_cards(team1_cards, team2_cards):
    """
    只比较红黄牌的总数量
    """
    team1_total = team1_cards['red'] + team1_cards['yellow']
    team2_total = team2_cards['red'] + team2_cards['yellow']
    print(f"队1总牌数: {team1_total} 张")
    print(f"队2总牌数: {team2_total} 张")
    if team1_total > team2_total:
        return "队1 红黄牌总数更多"
    elif team2_total > team1_total:
        return "队2 红黄牌总数更多"
    else:
        return "两队红黄牌总数相同"
# 示例数据
team1 = {'red': 2, 'yellow': 3}
team2 = {'red': 1, 'yellow': 4}
result = compare_total_cards(team1, team2)
print(result)

版本3:完整版(包含用户输入)

def collect_card_data():
    """收集用户输入的数据"""
    print("=== 输入球队1的红黄牌数据 ===")
    team1_red = int(input("队1红牌数量: "))
    team1_yellow = int(input("队1黄牌数量: "))
    print("\n=== 输入球队2的红黄牌数据 ===")
    team2_red = int(input("队2红牌数量: "))
    team2_yellow = int(input("队2黄牌数量: "))
    return (
        {'red': team1_red, 'yellow': team1_yellow},
        {'red': team2_red, 'yellow': team2_yellow}
    )
def analyze_cards():
    """分析红黄牌数据"""
    team1, team2 = collect_card_data()
    print("\n=== 分析结果 ===")
    # 按红牌比较
    print(f"红牌比较: 队1有{team1['red']}张,队2有{team2['red']}张")
    if team1['red'] > team2['red']:
        print("→ 队1红牌更多")
    elif team2['red'] > team1['red']:
        print("→ 队2红牌更多")
    else:
        print("→ 两队红牌数量相同")
    # 按黄牌比较
    print(f"黄牌比较: 队1有{team1['yellow']}张,队2有{team2['yellow']}张")
    if team1['yellow'] > team2['yellow']:
        print("→ 队1黄牌更多")
    elif team2['yellow'] > team1['yellow']:
        print("→ 队2黄牌更多")
    else:
        print("→ 两队黄牌数量相同")
    # 按总牌数比较
    team1_total = team1['red'] + team1['yellow']
    team2_total = team2['red'] + team2['yellow']
    print(f"\n总牌数: 队1有{team1_total}张,队2有{team2_total}张")
    if team1_total > team2_total:
        print("🏆 队1红黄牌总数更多")
    elif team2_total > team1_total:
        print("🏆 队2红黄牌总数更多")
    else:
        print("🏆 两队红黄牌总数相同")
# 运行程序
if __name__ == "__main__":
    analyze_cards()

版本4:面向对象版(更专业)

class Team:
    """球队类"""
    def __init__(self, name, red_cards=0, yellow_cards=0):
        self.name = name
        self.red_cards = red_cards
        self.yellow_cards = yellow_cards
    def total_cards(self):
        """返回总牌数"""
        return self.red_cards + self.yellow_cards
    def penalty_score(self):
        """返回罚分(红牌3分,黄牌1分)"""
        return self.red_cards * 3 + self.yellow_cards * 1
    def __str__(self):
        return f"{self.name}: {self.red_cards}红, {self.yellow_cards}黄, 总分{self.penalty_score()}"
def compare_teams(team1, team2):
    """比较两队"""
    print("=" * 40)
    print(team1)
    print(team2)
    print("=" * 40)
    # 按罚分比较
    if team1.penalty_score() > team2.penalty_score():
        print(f"🏆 {team1.name} 罚分更多")
    elif team2.penalty_score() > team1.penalty_score():
        print(f"🏆 {team2.name} 罚分更多")
    else:
        print("🏆 两队罚分相同")
# 使用示例
if __name__ == "__main__":
    # 创建球队对象
    team_a = Team("巴西", red_cards=2, yellow_cards=5)
    team_b = Team("德国", red_cards=1, yellow_cards=4)
    # 比较
    compare_teams(team_a, team_b)

运行效果示例

=== 分析结果 ===
红牌比较: 队1有2张,队2有1张
→ 队1红牌更多
黄牌比较: 队1有3张,队2有5张
→ 队2黄牌更多
总牌数: 队1有5张,队2有6张
🏆 队2红黄牌总数更多

使用建议

  1. 简单比较:使用版本1或版本2
  2. 交互式输入:使用版本3
  3. 需要扩展功能:使用版本4的面向对象方式

您可以根据具体需求选择合适的版本,如果需要修改比较逻辑(比如红牌权重更大),可以在代码中调整评分标准。

抱歉,评论功能暂时关闭!