![]() |
|
|
| Subject:
Python Programming Modify Student Class
Category: Computers > Programming Asked by: babutche-ga List Price: $65.00 |
Posted:
25 Nov 2006 12:55 PST
Expires: 25 Dec 2006 12:55 PST Question ID: 785517 |
Python Programming Question
Modify the student class by adding a mutator method that records a
grade for the student. Here is the specs for the new student:
addGrade(self, gradePoint, credits) gradePoint is a float that
represents a grade(e.g. A = 4.0, A- = 3.7, B+ = 3.3 etc.) and credits
is a float indicating the # of credit hours for the class. Modifies
the student object by adding this grade info.
Use the updated class to implement a simple program for calculating
GPA. Your program should create a new student object that has 0
credits and 0 quality points.(the name is irrelevant). Your program
should then prompt the user to enter course information(gradepoint,
credits) and then print out the final GPA acheived.
This is the student class to be modified:
class Student:
def __init__(self, name, hours, qpoints)
self.name = name
self.hours = float(hours)
self.qpoints = float(qpoints
def getName(self):
return self.name
def getHours(self):
return self.hours
def getQpoints(self):
return self.qpoints
def gpa(self):
return self.qpoints/self.hours
def makestudent(infoStr):
name, hours, qpoints = string.split(infoStr, "\t")
return Student(name, hours, qpoints)
def main():
filename = raw_input(Enter name the grade file: ")
infile = open(filename, 'r')
best = makeStudent(infile.readline())
for line in infile:
s = makeStudent(line)
if s.gpa() > best.gpa():
best = s
infile.close()
print "The best student is: "), best.getName() |
|
| Subject:
Re: Python Programming Modify Student Class
Answered By: leapinglizard-ga on 29 Nov 2006 18:03 PST |
Dear babutche,
The makestudent() and main() methods in your sample code are
irrelevant to the calculation of cumulative GPA, so I have omitted
them from my own code. I have included many comments in the code to
help you understand how the user feedback loop works.
I ran the program from a UNIX command line to obtain the following test transcript.
$ python student.py
Enter grade for next course, or nothing to finish: 3.0
Enter number of credit hours for this course: 10
Enter grade for next course, or nothing to finish: 4.0
Enter number of credit hours for this course: 30
Enter grade for next course, or nothing to finish: 3.7
Enter number of credit hours for this course: 20
Enter grade for next course, or nothing to finish: 3.3
Enter number of credit hours for this course: 20
Enter grade for next course, or nothing to finish:
*** final GPA = 3.625
The code listing is below.
Regards,
leapinglizard
#===begin student.py
class Student:
def __init__(self, name, hours, qpoints):
self.name = name
self.hours = float(hours)
self.qpoints = float(qpoints)
def getName(self):
return self.name
def getHours(self):
return self.hours
def getQpoints(self):
return self.qpoints
def gpa(self):
return self.qpoints/self.hours
# new mutator method as per specification
def addGrade(self, gradePoint, credits):
self.hours += credits
self.qpoints += credits*gradePoint
if __name__ == '__main__':
# output messages
prompt_grade = 'Enter grade for next course, or nothing 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 no grade is entered
if grade_str.strip() == '':
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.addGrade(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()
#===end student.py | |
| |
| |
|
| There are no comments at this time. |
If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you. |
| Search Google Answers for |
| Google Home - Answers FAQ - Terms of Service - Privacy Policy |