用Python实现以下内容:StuScore类实现记录学生的姓名及个各科成绩,该类有name和scoredict两个属性,其中scoredict是一个字典;方法__init__(self,name),传入学生姓名,并将其初始化为空字典;方法addScore(self,courseName,score),实现往scoredict添加课程名称及对应的成绩;方法getMaxScore(self),返回该生最高成绩对应的课程名称及成绩;方法getAvg(self)返回该生的平均成绩。将该类实例,多次调用addScore()方法为该对象添加若干课程及分数,调用getMaxScore()和getAvg()方法,打印输出该对象的最高成绩和平均成绩。认真研读以下代码,填空实现程序功能。 (1) 实验代码 class StuScore(): def __init__(self,name): self.name = name self.scoredict = {} def addScore(self,courseName,score): def getMaxScore(self): ls = [] if self.scoredict: max_score = for course,score in self.scoredict.items(): if : ls.append((course,score)) return ls def getAvg(self): avg = 0 if self.scoredict: avg = sum(self.scoredict.values())/len(self.scoredict.values()) May =StuScore("May") May.addScore("Chinese",90) May.addScore("Math",80) May.addScore("English",90) May.addScore("Computer",80) print(May.getMaxScore()) print(May.getAvg())
时间: 2024-01-26 15:04:42 浏览: 105
基于Python实现学生成绩管理系统【100010845】
5星 · 资源好评率100%
填空代码:
```python
class StuScore():
def __init__(self,name):
self.name = name
self.scoredict = {}
def addScore(self,courseName,score):
self.scoredict[courseName] = score
def getMaxScore(self):
ls = []
if self.scoredict:
max_score = max(self.scoredict.values())
for course,score in self.scoredict.items():
if score == max_score:
ls.append((course,score))
return ls
def getAvg(self):
avg = 0
if self.scoredict:
avg = sum(self.scoredict.values())/len(self.scoredict.values())
return avg
May = StuScore("May")
May.addScore("Chinese",90)
May.addScore("Math",80)
May.addScore("English",90)
May.addScore("Computer",80)
print(May.getMaxScore())
print(May.getAvg())
```
输出结果为:
```
[('Chinese', 90), ('English', 90)]
85.0
```
阅读全文