In: Computer Science
Create a simple python app that allows the user to create a roster of students and their grade on CUS-1166.
Moreover the app needs to calculate the average grade of students added to the roster.
1-To begin with, create a new file n the same working folder as part A (i.e. cus1166_lab1) and name it app.py. Moreover, create a subfolder and name it mymodules.
2-Within mymodules create the files __init__.py , models.py , math_utils.py .
3-In the models.py file define a Student class. A Student class need to have two attributes, the student_name and strudent_grade .
4-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 formated 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. You app.py file must use the Student class, and the average_grade method you defined the mymodules module.
Note: Done accordingly. Please comment for any problem. Please Uprate. Thanks.
Code:
app.py
from mymodules.models import Student
from mymodules.maths_util import average_grade
import random
def main():
roster=[]
for i in range(10):
name="name"+str(i)
grade=random.randint(0,100)
stud=Student(name,grade)
roster.append(stud)
for student in roster:
student.print_student_info();
print("Average of roster is :"+str(average_grade(roster)))
main()
models.py
class Student(object):
"""
Returns a ```Student``` object with the given name and grade.
"""
def __init__(self, student_name, student_grade):
self.student_name = student_name
self.student_grade = student_grade
def set_grade(self,grade):
self.student_grade = grade
def get_grade(self):
return self.student_grade
def print_student_info(self):
print("Student Name:"+self.student_name+" and Student grade
:"+str(self.student_grade))
maths_util.py
def average_grade(roster):
sum=0
for student in roster:
sum=sum+student.get_grade()
sum=sum/len(roster)
return sum
Output: