Python Programming Modify Student Class
-
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()
-
Answer:
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
babutche-ga at Google Answers Visit the source
Related Q & A:
- What are the common usage of python programming language?Best solution by quora.com
- What is the difference between a static method and class method in Python?Best solution by pythoncentral.io
- Is it correct to extend a bean class and write a class which holds logic to populate the bean we extend?Best solution by stackoverflow.com
- How to use an object created in Main class with another created in another class?Best solution by stackoverflow.com
- Can I modify the keys in a dictionary directly for Python?Best solution by stackoverflow.com
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.