【发布时间】:2021-06-06 06:04:06
【问题描述】:
我尝试创建类并定义其对象以找到总和和平均值但代码不起作用。我已经制作了 CricketGame 类并创建了模型和对象,但代码仍然无法正常工作。谁能让我更清楚地了解如何使用类对象和模型。
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
def playerstat(self):
batting_average = self.runs_scored/self.innings
batting_strike_rate = self.runs_scored/self.ball_faced
bowling_average = self.ball_bowled/self.wickets_taken
bowling_strike_rate = self.runs_given/self.wickets_taken
def playerresult(self, batting_average, batting_strike_rate, bowling_average, bowling_strike_rate):
print "Batting Average : ", batting_average
print "Batting Strike Rate : ", batting_strike_rate
print "Bowling Average : ", bowling_average
print "Bowling Strike rate : ", bowling_strike_rate
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
name = raw_input("Enter the name of the player : ")
age = input("Enter the age of the player : ")
matches = input("Enter the number of matches played : ")
innings = input("Enter the innings played : ")
runs_scored = input("Enter total runs scored: ")
wickets_taken = input("Enter the total wickets taken : ")
ball_faced = input("Enter the total number of ball faced : ")
runs_given = input("Enter the total runs given : ")
ball_bowled = 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()
【问题讨论】:
标签: python class object sum average