Please guys help me with this python code, make it very simple and easy for me p
ID: 672874 • Letter: P
Question
Please guys help me with this python code, make it very simple and easy for me please so I can explain it to the teacher easily.
1. I am biology teacher, and I want a program that will help compute student grades. When I entered -1, the program should stop, calculate the sum and give also give me the average. Also, a letter grade should be assigned to the student. (90 - 100 = A, 80-89 = B
70-79 = C, 60-69 = D, 60 - 0 = F.
Program Specification
1. use a function to get the student name
2. use a function to get the student grades
3. use a function to calculate the total of the grades
4. use a function to get the student grade letter
See output sample
****************************************************************************
**********Welcome to Biology 101 grade calculator*******************
Please, enter the name of the student: John Doe
Please, enter grade no 1 : 50
Please, enter grade no 2 : 50
Please, enter grade no 3 : 50
Please, enter grade no 4 : 50
Please, enter grade no 5 : 50
Please, enter grade no 6 : -1
You have entered 5 grades for John Doe
The total of the grades entered for John Doe is : 250
The average of John Doe = 50
The grade letter of John DOe = F
Explanation / Answer
***updated
from fileinput import input
def import_data(grades_file, assignments):
student_grades = dict()
for line in input(grades_file):
this_line = line.split()
student_grades[this_line[0]] = [int(item) for item in this_line[1:assignments + 1]]
return student_grades
def student_means(student_grades):
for k, v in student_grades.iteritems():
grades = v
grades_mean = float(sum(item for item in grades)) / len(grades)
v.append(grades_mean)
student_grades_mean = student_grades
return student_grades_mean
def class_mean(student_grades_mean):
class_grades = list()
for k, v in student_grades_mean.iteritems():
this_avg = v[-1]
class_grades.append(this_avg)
class_mean = float(sum(item for item in class_grades)) / len(class_grades)
return class_mean
def main():
grades_file = raw_input('File name: ')
assignments = int(raw_input('How many assignments are there? '))
student_data = import_data(grades_file, assignments)
student_data_avg = student_means(student_data)
class_avg = class_mean(student_data_avg)
print 'class average: %0.2f' % class_avg
for k, v in student_data_avg.iteritems():
grades = v[:-1]
grades = ' '.join(str(i) for i in grades)
avg = str(v[-1])
print 'name: %s | grades: %s | average: %s' % (k, grades, avg)
if __name__ == '__main__':
main()
from fileinput import input
def mean(list):
return sum(map(float, list)) / len(list)
if len(list) > 0 else float('nan')
def import_student_grades(grades_file):
line_words = [line.split() for line in input(grades_file)]
return dict((words[0], words[1:]) for words in line_words)
def main():
grades_file = raw_input('File name: ')
student_grades = import_student_grades(grades_file)
student_means = dict((k, mean(v)) for k, v in student_grades.iteritems())
print 'class average: %0.2f' % mean(student_means.values())
for name, grades in student_grades.iteritems():
print 'name: %(name)s | grades: %(grades)s | average: %(avg)0.2f' % {
'name': name,
'grades': ' '.join(grades),
'avg': student_means[name],
}
if __name__ == '__main__':
main()