Question: 1.10) Can you please help me with this Python question. A simple course management system models a students information with a name and a set
1.10)
Can you please help me with this Python question.
A simple course management system models a students information with a name and a set of test scores. This system should be able to create a student object with a given name and a number of scores, all of which will be 0 at startup.
The system should be able to access or replace a score at the given position (counting from 0):
- obtain the number of scores - getNumberOfScores()
- obtain the highest score - getHighScore()
- obtain the average score - getAverage()
- obtain the students name - getName()
In addition, the student object when printed should show the students name and scores as in the following example:
Name: Ken Lambert
Score 1: 88
Score 2: 77
Score 3: 100
High score: 100
Average: 88.333
Define a Student class that supports these features and behavior. A main() has been provided for you in order to test the implementation of your Student class.
#(Starting code)
# Write your code below:
def main(numScores = 3):
"""Tests sorting."""
# Create the list and put 5 students into it
students = list()
names = ("Juan", "Bill", "Stacy", "Maria", "Charley")
for name in names:
s = Student(name, numScores)
for index in range(numScores):
s.setScore(index, randint(70, 100))
students.append(s)
# Print the contents
print("The list of students:")
for s in students:
print(s)
if __name__ == "__main__":
main()
I have worked on it and it does work, BUT only got 40 %. Maybe i'm missing something.
Here is what i did ( Which is not right).
class Student:
def __init__(self,name,numScores):
self.name=name
self.numScores=numScores
self.Scores=[]
def setScore(self,index,score):
self.Scores.insert(index,score)
def getNumberOfScores(self):
return self.Scores;
def getHighScore(self):
return max(self.Scores)
def getAverage(self):
return sum(self.Scores)/numScores
def getName(self):
return self.name
import random
if __name__ == "__main__":
numScores=3
s=Student("Ken Lambert",numScores)
for i in range(numScores):
s.setScore(i,random.randint(70,100))
print("Name: ",s.getName())
result=s.getNumberOfScores();
for i in range(numScores):
print("Score",i+1,":",result[i])
print("High Score:",s.getHighScore())
print("Average is:",s.getAverage())
The Feedback i got.
- if you're going to refer to an instance variable you must include the self. at the start of it.
- When returning the getNumberOfScores you are returning the list itself when you should be returning the length of the list
- When setting a score, you aren't inserting you are replacing what is there. So first when you create the student object it should loop and assign 0 for all the grades. When setting a score, you just access the list at the index given and assign it the grade given.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
