Working with python . Class Grade 1. The constructor (method __init__) will acce
ID: 3572008 • Letter: W
Question
Working with python.
Class Grade
1. The constructor (method __init__) will accept 3 values as parameters, name of the assignment (string), grade and assignment weight (floats). All three parameters must have default values—you choose appropriate ones. Also, the attributes cannot be private because you need to access them in the Student class. That is, do not use double underscores in your names, i.e. do not use an attribute name such as self.__name.
2. The string method (__str__) and representation method (__repr__) should return a string that includes the name, grade and weight of the Grade object in one line.
Class Student
1. The constructor (method __init__) will accept 4 values as parameters, student id (int), first name (string), last name (string), and a list of all the grade objects for that student. All three parameters must have default values—you choose appropriate ones, but the appropriate default value for a list parameter is None, not an empty list ([]).
2. add_grade method should take a Grade object as a parameter and append it to a list of grades for that student.
3. calculate_grade should multiply all the grade points of the student by their weight and return the final course grade. (Each Grade object in the list has a value and weight that should be multiplied together. The sum of all the calculations results the final weighted grade of the student in the course)
4. The string method (__str__) and representation method (__repr__) should return a string that has the student’s name (with last name first with a comma between the last name and first name) includes all the grades of the student. This method must call the __str__ method of the Grade objects. Note that implicitly calling the __str__ method is fine such as str() or format(), but do not access the individual attributes such as grade.name.
5. __gt__, __lt__ and __eq__ methods should compare the final grades of two students together.
Note that you can use calculate_grade method to calculate the final grade of two students. Never use “==” to check for equality of two float numbers (because floats are approximations of real numbers). Instead, find the absolute value of the difference of two numbers; if the difference is less than epsilon (a very small number such as 10**-6), the floats are equal to each other (that is they are close enough to consider them to be equal). Python has an absolute value function: abs()
Explanation / Answer
Class Grade
class Gpa(object):
# data attributes
"helps to calculate the Gpa and Cgpa"
arg1 = None
arg2 = None
subData = None
Scale = None
credits = None
initCourse = 0
initgetCredit = 0
totalCredits = 0
temp = 0
def getCourse(self):
"get the value of the no of course you registered"
self.arg1 = input("No of course you have registered: " )
pass
def getSubject(self,value):
"get the subject value"
self.arg2 = value
pass
def getScale(self):
"To get the scale value"
self.Scale = input("Enter the Scale value(Either 5 or 10): " )
pass
def getSubjectData(self):
"get the subject Data in string"
self.subData = raw_input("Enter the grade: " )
pass
def getGradeData(self):
# To calculate grade for two scale,one is for 5.0 and other one for 10.0
if self.Scale == 10:
grade1 = {'s':10,'a':9,'b':8,'c':7,'d':5,'e':3,'f':0}
x=grade1[self.subData]
else: #5.0 scale
grade2 = {'a':5,'b':4,'c':3,'d':2,'e':1,'f':0}
x=grade2[self.subData]
return x
def getCredits(self):
"get credit value"
self.credits = input("Enter the credits for a subject:" )
pass
def gpa(self):
print "Calculate GPA:"
sem = raw_input("Please Enter Semester: " )
self.getScale() #input the scale value
if self.Scale == 5 or self.Scale == 10:
self.getCourse()
if self.arg1 >= 2:
self.calculateGpa()
else:
print "In order to calculate Gpa you schould have atleast 2 subject minimum"
else:
print "you have not entered the scale correctly please try again"
pass
def calculateGpa(self):
"Method to calculate Gpa "
while self.initCourse!=self.arg1:
self.initCourse=self.initCourse+1
self.getCredits()
self.initgetCredit = self.credits
self.getSubjectData()
#type(self.getSubjectData())
self.temp = self.initgetCredit*self.getGradeData()+self.temp
self.totalCredits=self.totalCredits+self.initgetCredit
gpa = round((self.temp+.0)/(self.totalCredits+.0),2)
print "you have registered for total credits:"+" "+str(self.totalCredits)+" "+"and you have acquired GPA:""+str(gpa)+"""
pass
def cgpa(self):
print "Calculate your cgpa: "
semesters = input("Enter how many semester cgpa has to be found of: " )
counter = 0
tempInit = 0
tempTotalCredits = 0
self.getScale() #input the scale value
if self.Scale == 5 or self.Scale == 10:
while counter != semesters:
counter = counter+1
print "Please enter the details of the semester"+" "+str(counter)
self.getCourse()
self.calculateGpa()
tempInit = self.temp+tempInit
tempTotalCredits = tempTotalCredits + self.totalCredits
# re-assigning
self.arg1=0
self.initCourse =0
self.temp=0
self.totalCredits=0
print " "
cgpa = round((tempInit+.0)/(tempTotalCredits+.0),2)
print "you have registered for total credits:"+" "+str(tempTotalCredits)+" "+"and you have acquired CGPA:""+str(cgpa)+"" "
else:
print "you have not entered the scale correctly please try again"
pass
if __name__ == '__main__': # main method
#how to calculate it
Init = Gpa() # Creating Instance
# for calculation of Cgpa (cumulative grade point average)
Init.cgpa()
# In Order to calculate Gpa for single semester
#Init.gpa()
Class Student
print " Welcome to the grading program. "
good = 0
fail = 0
students = 0
totalscore = 0
while student == "y"
name = raw_input("What is your name; ")
grade = int(raw_input("What is your grade (0-100): "))
students += 1
totalscore = totalscore + grade
if grade > 90:
good += 1
print "You are doing extremely well."
elif grade < 60:
fail += 1
print "You are in jeopardy of failing."
student = raw_input("Are there any more students in your class (y/n): ")
average = totalscore /= students
print "There are" , students , "in this class."
print "There are" , good , "students doing extremely well and there are" , fail , "students who are in jeopardy
of failing."
print "The average score of the class is" , average , "."
print raw_input("Press Enter to Exit Program")
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.