python案例统计快发任意球尝试几次?

wen python案例 3

本文目录导读:

python案例统计快发任意球尝试几次?

  1. 方案1:基础版 - 使用列表统计
  2. 方案2:使用字典和类
  3. 方案3:更复杂的比赛数据统计
  4. 方案4:实时数据输入版
  5. 使用建议

我可以帮你编写一个统计任意球尝试次数的Python程序,不过我注意到你说的是"快发任意球",这在足球规则中是指快速罚球的方式,让我为你设计一个解决方案:

方案1:基础版 - 使用列表统计

def count_free_kick_attempts(events):
    """
    统计任意球尝试次数
    events: 比赛事件列表,包含字符串描述
    """
    free_kick_count = 0
    fast_free_kick_count = 0
    for event in events:
        # 判断是否为任意球
        if "任意球" in event:
            free_kick_count += 1
            # 判断是否为快发任意球
            if "快发" in event:
                fast_free_kick_count += 1
    return free_kick_count, fast_free_kick_count
# 示例数据
match_events = [
    "常规任意球",
    "快发任意球",
    "常规任意球",
    "快发任意球",
    "快发任意球",
    "角球",
    "常规任意球",
]
total, fast = count_free_kick_attempts(match_events)
print(f"任意球总尝试次数: {total}")
print(f"快发任意球尝试次数: {fast}")

方案2:使用字典和类

class FreeKickTracker:
    def __init__(self):
        self.attempts = {'常规任意球': 0, '快发任意球': 0}
        self.total_attempts = 0
    def record_attempt(self, kick_type):
        """记录一次任意球尝试"""
        self.total_attempts += 1
        if kick_type in self.attempts:
            self.attempts[kick_type] += 1
    def get_statistics(self):
        """获取统计信息"""
        print("=== 任意球统计 ===")
        print(f"总尝试次数: {self.total_attempts}")
        print(f"常规任意球: {self.attempts['常规任意球']}次")
        print(f"快发任意球: {self.attempts['快发任意球']}次")
        print(f"快发比例: {self.attempts['快发任意球']/self.total_attempts*100:.1f}%")
# 使用示例
tracker = FreeKickTracker()
# 模拟比赛中的任意球事件
tracker.record_attempt("常规任意球")
tracker.record_attempt("快发任意球")
tracker.record_attempt("快发任意球")
tracker.record_attempt("常规任意球")
tracker.record_attempt("快发任意球")
tracker.get_statistics()

方案3:更复杂的比赛数据统计

import random
from datetime import datetime
def simulate_match_free_kicks(num_matches=10):
    """模拟多场比赛的任意球数据"""
    all_matches = []
    for match_id in range(1, num_matches + 1):
        match_data = {
            'match_id': match_id,
            'free_kicks': []
        }
        # 模拟每场比赛的任意球次数(5-15次)
        num_free_kicks = random.randint(5, 15)
        for _ in range(num_free_kicks):
            kick = {
                'minute': random.randint(1, 95),  # 包含补时
                'is_fast': random.choice([True, False, False]),  # 30%概率是快发
                'is_goal': random.random() < 0.1,  # 10%概率进球
                'taker': f"球员{random.randint(1,11)}"
            }
            match_data['free_kicks'].append(kick)
        all_matches.append(match_data)
    return all_matches
def analyze_free_kicks(matches_data):
    """分析任意球数据"""
    total_fast = 0
    total_normal = 0
    total_goals = 0
    fast_goals = 0
    for match in matches_data:
        for kick in match['free_kicks']:
            if kick['is_fast']:
                total_fast += 1
                if kick['is_goal']:
                    fast_goals += 1
            else:
                total_normal += 1
                if kick['is_goal']:
                    total_goals += 1
    total_attempts = total_fast + total_normal
    print(f"快发任意球次数: {total_fast}")
    print(f"常规任意球次数: {total_normal}")
    print(f"总任意球次数: {total_attempts}")
    print(f"快发成功率: {fast_goals/total_fast*100:.1f}%")
    print(f"常规成功率: {total_goals/total_normal*100:.1f}%")
# 运行分析
matches = simulate_match_free_kicks(5)
analyze_free_kicks(matches)

方案4:实时数据输入版

def real_time_free_kick_counter():
    """实时输入统计任意球"""
    fast_count = 0
    normal_count = 0
    total_count = 0
    print("=== 实时任意球统计系统 ===")
    print("输入'F'记录快发任意球")
    print("输入'N'记录常规任意球")
    print("输入'S'和'Q'查看统计/退出")
    while True:
        action = input("输入操作: ").upper().strip()
        if action == 'F':
            total_count += 1
            fast_count += 1
            print(f"✓ 记录快发任意球 (当前: 快发{fast_count}次)")
        elif action == 'N':
            total_count += 1
            normal_count += 1
            print(f"✓ 记录常规任意球 (当前: 常规{normal_count}次)")
        elif action == 'S':
            print("\n=== 当前统计 ===")
            print(f"总尝试: {total_count}次")
            print(f"快发: {fast_count}次")
            print(f"常规: {normal_count}次")
        elif action == 'Q':
            print("统计结束!")
            break
        else:
            print("无效输入,请重试")
# 运行实时统计
# real_time_free_kick_counter()

使用建议

  1. 简单统计:使用方案1或2,适合快速统计
  2. 数据分析:使用方案3,适合分析大量比赛数据
  3. 实时记录:使用方案4,适合比赛现场统计

你可以根据具体需求选择合适的方法,需要我解释代码的某个部分,或者帮你定制更符合需求的版本吗?

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