You need to enable JavaScript to run this app. 如何使用Python类对象接收用户输入计算总和与平均值及调试类代码
优惠活动
大模型
产品
解决方案
定价
更多

如何使用Python类对象接收用户输入计算总和与平均值及调试类代码

类、对象基础概念
  • 类是抽象的功能模板,你定义的CricketGame就是模板,统一封装了一类对象的公共属性(姓名、年龄、参赛数据等)和方法(计算统计值、打印信息等)
  • 对象是类的实例化实体,你代码最后创建的player就是CricketGame模板生成的具体球员对象,自带类中定义的所有属性和方法
  • self是类内部指代当前实例的关键字,属性只要绑定到self上,就能在类的所有方法之间共享,没有绑定到self的临时局部变量,方法执行完就会被销毁,其他方法无法访问
代码错误点
  1. playerstat方法中计算的四个指标都是临时局部变量,没有绑定到self,调用完该方法后数值直接丢失,后续playerresult无法获取到计算结果
  2. playerresult定义时要求传入4个参数,但你调用时写的是player.playerresult()没有传参,会直接触发参数不匹配报错
  3. 用了Python2的过时语法:print后没有括号、raw_input在Python3中已被移除,如果运行环境是Python3会直接报错
  4. 没有除零校验,比如当球员没拿到过三柱门时wickets_taken为0,计算投球相关指标会触发除零报错
修正后的可运行代码
class CricketGame:
    def __init__(self, name, age, matches, innings, runs_scored, wickets_taken, ball_faced, runs_given, ball_bowled):
        self.name = name
        self.age = age
        self.matches = matches
        self.innings = innings
        self.runs_scored = runs_scored
        self.wickets_taken = wickets_taken
        self.ball_faced = ball_faced
        self.runs_given = runs_given
        self.ball_bowled = ball_bowled
        # 初始化统计属性
        self.batting_average = 0
        self.batting_strike_rate = 0
        self.bowling_average = 0
        self.bowling_strike_rate = 0

    def playerstat(self):
        # 增加除零判断避免报错
        if self.innings > 0:
            self.batting_average = self.runs_scored / self.innings
        if self.ball_faced > 0:
            self.batting_strike_rate = self.runs_scored / self.ball_faced
        if self.wickets_taken > 0:
            self.bowling_average = self.ball_bowled / self.wickets_taken
            self.bowling_strike_rate = self.runs_given / self.wickets_taken

    def playerresult(self):
        # 直接从当前实例取计算好的统计值,不需要额外传参
        print("Batting Average : ", round(self.batting_average, 2))
        print("Batting Strike Rate : ", round(self.batting_strike_rate, 2))
        print("Bowling Average : ", round(self.bowling_average, 2))
        print("Bowling Strike rate : ", round(self.bowling_strike_rate, 2))

    def playerinfo(self):
        print("Player's Name : ", self.name)
        print("Age : ", self.age)
        print("Matches : ", self.matches)
        print("Inning Played :", self.innings)
        print("Run Score : ", self.runs_scored)
        print("Wicket Taken :", self.wickets_taken)
        print("Ball Faced :", self.ball_faced)
        print("Runs Given : ", self.runs_given)
        print("Ball Bowled : ", self.ball_bowled)


# 适配Python3输入语法,手动转换数值类型
name = input("Enter the name of the player : ")
age = int(input("Enter the age of the player : "))
matches = int(input("Enter the number of matches played : "))
innings = int(input("Enter the innings played : "))
runs_scored = int(input("Enter total runs scored: "))
wickets_taken = int(input("Enter the total wickets taken : "))
ball_faced = int(input("Enter the total number of ball faced : "))
runs_given = int(input("Enter the total runs given : "))
ball_bowled = int(input("Enter the total ball bowled : "))

player = CricketGame(name, age, matches, innings, runs_scored, wickets_taken, ball_faced, runs_given, ball_bowled)
player.playerinfo()
player.playerstat()
player.playerresult()

内容的提问来源于stack exchange,提问作者vim vai

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.18 04:45:11