in this home work we are using python 2.7(basics) and sqlite3 Add a method to St
ID: 3834085 • Letter: I
Question
in this home work we are using python 2.7(basics) and sqlite3
Add a method to Student' to record (accumulate) the student's quality points and credits taken for each course information entered. The specification for this method is: add Course Info(self, grade, credits) Use the updated class to write code for calculating a student's overall GPA The program should create a new student object that has zero credits and a zero grade points (use your name for this object). The program should continually (use a loop) ask the user to enter course information (grade point and credits) for each course the student has taken. The user should be told to enter 'quit' when they are finished entering course information The program should print the student's total credit hours completed AND their overall GPA.Explanation / Answer
class Student:
def __init__(self, name, grade, credits):
self.name = name
self.grade = float(grade)
self.credits = float(credits)
def getName(self):
return self.name
def getHours(self):
return self.grade
def getQpoints(self):
return self.credits
def gpa(self):
return self.credits/self.grade
# new mutator method as per specification
def addCourceInfo(self, grade, credits):
self.grade += credits
self.credits += credits*grade
if __name__ == '__main__':
# output messages
prompt_grade = 'Enter grade for next course[quit to finish]: '
prompt_credits = 'Enter number of credit hours for this course: '
error_float = 'error: expected a floating-point number'
# make a new Student object
stu = Student('stu', 0.0, 0.0)
# user-feedback loop
while 1:
# prompt user to enter a grade
grade_str = raw_input(prompt_grade)
# quit if quit is entered
if grade_str.strip() == 'quit':
break
try:
# convert input to a floating-point value
grade = float(grade_str)
except ValueError:
# if input cannot be converted, restart feedback loop
print error_float
continue
# prompt user to enter the number of credits
credits_str = raw_input(prompt_credits).strip()
try:
# convert input to a floating-point value
credits = float(credits_str)
except ValueError:
# if input cannot be converted, restart feedback loop
print error_float
continue
# update the student's grades
stu.addCourceInfo(grade, credits)
# after user has entered all grades, compute the cumulative GPA
if stu.getHours() == 0.0:
# can't compute GPA if hours = 0
print '*** zero credit hours recorded'
else:
# otherwise, output cumulative GPA and finish
print 'final GPA =', stu.gpa()
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.