The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> Python Programming
|
Help!
Discuss Help! in the Python Programming forum on Dev Shed. Help! Python Programming forum discussing coding techniques, tips and tricks, and Zope related information. Python was designed from the ground up to be a completely object-oriented programming language.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

November 29th, 2012, 01:46 PM
|
|
Registered User
|
|
Join Date: Nov 2012
Posts: 16
Time spent in forums: 2 h 41 m 15 sec
Reputation 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())
|

November 29th, 2012, 01:55 PM
|
|
Registered User
|
|
Join Date: Nov 2012
Posts: 16
Time spent in forums: 2 h 41 m 15 sec
Reputation 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")
|

November 29th, 2012, 03:30 PM
|
 |
Contributing User
|
|
|
|
|
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!
|

November 29th, 2012, 04:02 PM
|
 |
Contributing User
|
|
|
|
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))]
|

November 29th, 2012, 07:53 PM
|
|
Contributing User
|
|
Join Date: May 2009
Posts: 291
  
Time spent in forums: 3 Days 18 h 33 m 44 sec
Reputation Power: 7
|
|
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
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|