COSC 1336 Fundamentals of Programming Kathryn Rehfield Exam 3 Programming module
ID: 3837116 • Letter: C
Question
COSC 1336 Fundamentals of Programming Kathryn Rehfield Exam 3 Programming module for the class definition The class named CourseGrade has 4 private data attributes for I. Student's name Exam 2 score 4. Exam 3 score The class will have accessor and mutator methods for each of the data attributes. The class with have a method "get average" that calculates and returns the average of the 3 exam scores. The class wil have an init method that walinitialze the name to an empty string and the three exam scores to 0.0. The class wil have the special str method that returns the object state write a program to process grades for a class with 3 students. Your program wil prompt the user for the student's name and each of the 3 grades for all 3 students and store them in a list of Course Grade objects. program will pass the list of objects to a function that wil display the student's name, three exam scores and the average. You decide how to display the output. Include a comment at the top of both of your files with your name. You do not need to include other comments in your program or class module. Name your class module coursegrade Lastname.py. Name your source code file exam3 Lastname.py. Submit both files via the Exam 3 Program assignment.Explanation / Answer
class CorseGrade(object):
def __init__(self, name):
self.name, self.score = name, []
def append_grade(self, grade):
self.score.append(grade)
def get_average(self):
return sum(self.score) / len(self.score) #method to calculate average of examscore
def main():
print()
print('students information')
clss = []
while True:
print()
print('{} student in class '.format(len(clss)))
another_CorseGrade = input('new student to enter yes or no press y or n ? ')#taking student information
if another_CorseGrade[0].lower() != 'y': #check if y then add new student data andexam
break
print()
CorseGrade_name = input('enter student's name? ')# enter student name
print('-------------------')
clss.append(CorseGrade(CorseGrade_name))#thsi append method append name
print()
print('student name :', CorseGrade_name)
print('-------------------')
number_of_tests = int(input('Total Exam-number : '))#ask for totalexam
for number_of_exam in range(1, number_of_tests+1):
print('Examscore {}'.format(number_of_exam), end='')#for loop to check given examno and execute till that no and ask for examscore
score = float(input(' : '))
if score < 0: #stop is less than 0
break
clss[-1].append_grade(score) # append to last which isnext number for exammark
print_report(clss)
def print_report(clss):
print()
print('-------------------')
print('Average score')
print('-------------------')
print()
for CorseGrade in sorted(clss, key=lambda s: s.name): #print using for loop all student name and their respective average for three score
print('student name: {:20s} average Exam-score: {:3.2f} '.format(
CorseGrade.name, CorseGrade.get_average()))
print()
print('The three studnet get_average is {:.2f}'.format(class_get_average(clss)))
main()
share2