Thread: Help!

    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    16
    Rep Power
    0

    Help!


    Here is the assignment:

    For this assignment, you must implement a course grading program. Your program should read from an input file called "grades.txt" of the following format:
    2
    John Doe
    82
    100
    57
    0
    Jane Smith
    91
    12
    45
    81
    0
    The first number represents the total number of students included in the file. The number of grades for each student is unknown. The zero signifies the end of a student's grades. Your program should calculate the average of each student’s grades and output the following to the screen (with the grades now sorted, ascending):
    John Doe 57, 82, 100 -- B
    Jane Smith 12, 45, 81, 91 -- A
    Your program should write to an output file called “summary.txt” a summary like the following:
    Number of students: 12
    Average grade: 72.5
    Highest grade: 97
    Lowest grade: 34
    Student with longest name: Jane Smith
    You must create a class called Student that represents a student's information. This class must contain attributes for the first name, last name, and list of grades. It must override the __str__ method that prints the student information as it should be displayed to the screen (see above). It must also provide a method called get_average that returns the average grade for the student, as well as a method called get_letter that returns the letter grade representation of the student's average.


    This is what I have:

    Code:
    class Student(object):
        def __init__(self, name=""):
            self.name = name
            self.grades = []
        def get_average(self):
            return sum(self.grades) / float(len(self.grades))
        def get_letter(self):
            if get_average >= 89.5:
                return 'A'
            elif 79.5 <= get_everage <= 89.4:
                return 'B'
            elif 69.5 <= get_everage <= 79.4:
                return 'C'
            elif 59.5 <= get_everage <= 69.4:
                return 'D'
            elif get_average <= 59.4:
                return 'F'
        def __str__(self):
            return "%s %0.2f %s" % (self.name, self.get_average(), self.get_letter())
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    16
    Rep Power
    0

    update:


    This is now what I have:

    Code:
    def class_student(object):
    	def init(self,name,grades):
    		self.name=name
    def str(self):
    	string=self.name
    	for i in range(len(list)):
    		string+=str(grades[i])
    	string+="__"+ getLetter()
    	return string
    def get_average(self):
    	for i in range(len(list)):
    		avg+=int(list[i])
    		avg1=len(list)
    	return avg
    def get_letter(self,avg):
    	if avg >89:
    		return"A"
    	elif avg == 89 and avg >79:
    		return"B"
    	if avg <79 and avg>69:
    		return"C"
    	elif avg <=69 and avg>59:
    		return"D"
    	else:
    		return"F"
    infile =open("grades.txt", "r")
    gradeslist = infile.readlines()
    students=gradeslist[0]
    i=1 
    stuList=[]
    for i in range(len(gradeslist)):
    	students=students(gradeslist[i])
    	grade=-1
    	while true:
    		i+=1
    		grades =gradeslist[i]
    	if grades == 0:
    		break
    else:
    	students.addGrade(gradeslist[i])
    student.sort(Grades())
    stuList.append(student)
    for i in range(studList):
    print str(stuList[i])
    outfile=open("Summary.txt", "W")
    outfile.write("number fo Students:"+numstudents+"\n")
  4. #3
  5. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,966
    Rep Power
    481

    retrograde programming


    blobman23! What's the deal? You retrogress. What happened to your class?

    Check out this version of get_letter!

    Code:
    def get_letter(avg):
        return 'ABCDF'[sum(avg <= cutoff for cutoff in range(60,100,10))]
    [code]Code tags[/code] are essential for python code and Makefiles!
  6. #4
  7. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,966
    Rep Power
    481
    Code:
    class Student:
    
        def __init__(self,first_name,last_name,grades):
            self.first_name = first_name
            self.last_name = last_name
            grades.sort()
            self.grades = grades
    
        def __str__(self):
            return '%s %s %s -- %s'%(
                self.first_name,self.last_name,
                str(self.grades)[1:-1],
                self.get_letter())
    
        def get_average(self):
            a = self.grades
            return sum(a)/float(len(a))
    
        def get_letter(self):
            a = self.get_average()
            return 'ABCDF'[sum(a <= cutoff for cutoff in range(60,100,10))]
    [code]Code tags[/code] are essential for python code and Makefiles!
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    May 2009
    Posts
    524
    Rep Power
    34
    I'm not sure if you got how to split the records on each student. It is pretty standard stuff when dealing with files. This creates a list of lists, with a sub-list for each student.
    Code:
    test_data="""2
    John Doe
    82
    100
    57
    0
    Jane Smith
    91
    12
    45
    81
    0
    Buggs Bunny
    92
    13
    46
    82
    0"""
    
    readlines_test=test_data.split("\n")
    print readlines_test
    
    list_o_students = []
    junk_list = []
    for ctr in range(1, len(readlines_test)):
        rec=readlines_test[ctr].strip()
        if "0" == rec[0]:
            if len(junk_list):
                list_o_students.append(junk_list)
            junk_list = []
        else:
            junk_list.append(rec)
    
    if len(junk_list):     ## append final group if no zero
        list_o_students.append(junk_list)
    
    print "-"*50
    for each_list in list_o_students:
        print each_list
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2014
    Posts
    4
    Rep Power
    0
    Originally Posted by b49P23TIvg
    Code:
    class Student:
    
        def __init__(self,first_name,last_name,grades):
            self.first_name = first_name
            self.last_name = last_name
            grades.sort()
            self.grades = grades
    
        def __str__(self):
            return '%s %s %s -- %s'%(
                self.first_name,self.last_name,
                str(self.grades)[1:-1],
                self.get_letter())
    
        def get_average(self):
            a = self.grades
            return sum(a)/float(len(a))
    
        def get_letter(self):
            a = self.get_average()
            return 'ABCDF'[sum(a <= cutoff for cutoff in range(60,100,10))]
    So after coding the function, can you help me write the output to the file "summary.txt", please?
  12. #7
  13. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,966
    Rep Power
    481
    Firstly you should know that blobman23 was dismissed from college when they found out he turned in his plagiarized assignment, loosing both tuition and student housing payments.
    [code]Code tags[/code] are essential for python code and Makefiles!
  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2014
    Posts
    4
    Rep Power
    0
    I don't need you to write for me the whole code, just guide me to write an output part.
  16. #9
  17. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,966
    Rep Power
    481
    Originally Posted by instruction
    __str__ method that prints the student information as it should be displayed to the screen
    Interesting, if I had a __str__ methods that prints the purpose would be for debugging. __str__ returns str.

    Code:
    import sys
    
    test_data = '''2
    John Doe
    82
    100
    57
    0
    Jane Smith
    91
    12
    45
    81
    0
    Buggs Bunny
    92
    13
    46
    82
    0'''
    
    class Student:
    
        def __init__(self,first_name,last_name,grades):
            self.first_name = first_name
            self.last_name = last_name
            grades.sort()
            self.grades = grades
    
        def __str__(self):
            return '%s %s %s -- %s'%(
                self.first_name,self.last_name,
                str(self.grades)[1:-1],
                self.get_letter())
    
        def get_average(self):
            a = self.grades
            return sum(a)/float(len(a))
    
        def get_letter(self):
            a = self.get_average()
            return 'ABCDF'[sum(a <= cutoff for cutoff in range(60,100,10))]
    
    if (__name__ == '__main__') and (sys.version[0] == '3'):
        import io
        students = []
        master_student = Student('', '', [])
        with io.StringIO(test_data) as inf:
            number_of_students = int(inf.readline())
            for i in range(number_of_students):
                (first, last,) = inf.readline().strip().split()
                grades = []
                while (not grades) or grades[-1]:
                    grades.append(int(inf.readline()))
                grades.sort()
                students.append(Student(first, last, grades[1:]))
                print(students[-1])
                master_student.grades.extend(grades[1:])
                if len(master_student.first_name) + len(master_student.last_name) < len(first) + len(last):
                    (master_student.first_name, master_student.last_name,) = (first, last,)
        master_student.grades.sort()
        print('')
        print(master_student)
        print('')
        print('worst numerical grade: {}'.format(master_student.grades[0]))
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo