In: Computer Science
Using Python
Define a Student class. A Student class needs to have two attributes, the student_name and strudent_grade . It also has three methods as follows: set_grade(grade) that sets the grade of the student. get_grade returns the grade of the student. print_student_info that print the name and grade of student in a formatted string.
In the math_utils.py file define a function called average_grade(roster) . This method accepts as input a list of Student Objects and returns the average of the current roster.
In the app.py file, create a main function, to create a roster of 10 students, print the list of students, and print the average score of the current roster. You can either populate the student roster interactively (i.e. by using the input method to prompt the user from the console) or hard-code the student information in your code.
The app.py file must use the Student class, and the average_grade method you defined the mymodules module.
Here is the completed code for this problem. Ensure that filenames are correct and all files are in same folder. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks
#code
#student.py file
#Student class
class Student:
#constructor taking name and grade
def __init__(self, name,
grade):
self.student_name=name
self.student_grade=grade
#sets the grade
def set_grade(self,
grade):
self.student_grade=grade
#returns the grade
def get_grade(self):
return
self.student_grade
#prints student info
def print_student_info
(self):
print('Name: {},
grade: {}'.format(self.student_name,
self.student_grade))
#math_utils.py file
#returns the average grade of all students in roster
def average_grade(roster):
#initializing total to 0
total=0
#looping and summing grades of all students
in roster
for student
in roster:
total+=student.get_grade()
#finding average and returning it
avg=total/len(roster)
return avg
#app.py file
#importing all classes/methods from student.py and math_utils.py
modules
#ensure that you have saved the Student class and average_grade
method in
#proper modules with correct name
from student import *
from math_utils import *
#main method
def main():
#creating a list of 10 students
roster =
[Student('Oliver', 90),
Student('John', 92),
Student('Nidhi', 94),
Student('Ram', 88),
Student('Kripke', 76),
Student('Raj', 96),
Student('Sheldon', 100),
Student('Penny', 75),
Student('Amy', 95),
Student('Barry', 79)]
#looping and printing all students
for i in
roster:
i.print_student_info()
#finding and printing average_grade
print('\nAverage
grade: {:.2f}'.format(average_grade(roster)))
main()
#output
Name: Oliver, grade: 90
Name: John, grade: 92
Name: Nidhi, grade: 94
Name: Ram, grade: 88
Name: Kripke, grade: 76
Name: Raj, grade: 96
Name: Sheldon, grade: 100
Name: Penny, grade: 75
Name: Amy, grade: 95
Name: Barry, grade: 79
Average grade: 88.50