Google Answers Logo
View Question
 
Q: Python Programming Modify Student Class ( Answered,   0 Comments )
Question  
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()
Answer  
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

Request for Answer Clarification by babutche-ga on 30 Nov 2006 00:36 PST
Isn't there suppose to be a def main:  in the program somewhere? 
Sorry but I am trying to learn Python and I really know very little
about it. Thanks!

Clarification of Answer by leapinglizard-ga on 30 Nov 2006 05:26 PST
There is no need to write a main() in a Python script. Unlike other
languages, Python does not automatically invoke main() or any other
function at runtime.

On the other hand, a script can carry out the test

 if __name__ == '__main__':

to determine if it is being run as a command, rather than being
imported as a library. So if you want something to be done
automatically at runtime but not at importation, you should put it in
such a test block.

If you want your Python script to look more like a C program, you can
always put your runtime functionality in a main() function and then
include

 if __name__ == '__main__':
     main()

at the end.

leapinglizard
Comments  
There are no comments at this time.

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

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 Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy