概述
我目前正在尝试计算由类中的方法创建的列表的平均值。首先将所有信息传递给一个类,该类记录/返回从main函数传递的数据。问题是我要从main函数传入什么来首先检索self.u marks列表,然后操作它以返回平均值。另外,我是否为calculatevaragemark部分使用了正确的代码?提前谢谢
代码如下:class Student :
def __init__(self, id):
self._studentId = id
self._marks = []
##
# Converts the student to a string .
# @return The string representation .
#
# Sets the student's ID.
# @param newId is the student's new ID.
#
def setStudentId(self, id):
self._studentId = id
##
# Gets the student's ID.
# @return the student's ID
#
def getStudentId(self, newId):
return self._newId
##
# appends a mark to the marks list
#
def addMark(self, mark):
self._marks.append(mark)
def __repr__(self) :
# Build a string starting with the student ID
# followed by the details of each mark .
s = "Student ID :" + self._studentId + " "
if len(self._marks) == 0 :
s += " "
else :
for mark in self._marks :
s += " Course Module: " + mark.getModule() + " Raw Mark: " + str(mark.getRawMark())
return s
##
# Method to calculate the students average mark
#
def calculateAverageMark(self):
totalMarks = 0
count = 0
for mark in self._marks :
if mark == 0 :
count = count
else :
count = count + 1
totalMarks = totalMarks + mark
average = totalMarks / count
return average
最后
以上就是炙热眼神为你收集整理的python求平均工资_在Python3中计算列表的平均值的全部内容,希望文章能够帮你解决python求平均工资_在Python3中计算列表的平均值所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复