SHARE
SPREAD
HELP

The Tradition of Sharing

Help your friends and juniors by posting answers to the questions that you know. Also post questions that are not available.


To start with, Sr2Jr’s first step is to reduce the expenses related to education. To achieve this goal Sr2Jr organized the textbook’s question and answers. Sr2Jr is community based and need your support to fill the question and answers. The question and answers posted will be available free of cost to all.

 

#
Authors:
Y Daniel Lang
Chapter:
0.lists
Exercise:
Programming Excercises
Question:1 | ISBN:978013274719 | Edition: 6

Question

(Assign grades) Write a program that reads a list of scores and then assigns grades based on the following scheme:
The grade is A if score is >=  best – 10.
The grade is B if score is  >=  best – 10.   .
The grade is C if score is  >=  best – 30.
The grade is D if score is >=  best – 40.
The grade is F otherwise.
Here is a sample run:

Enter scores: 40 55 70 58
Student  0 score  is 40  and  grade  is  C
Student  1 score i s 55  and  grade  is  B
Student  2 score is  70  and  grade  is  A
Student  3 score is  58  and  grade  is  B
 

TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Answer

#Prompt the user to enter the scores of total number of students
scores = list(map(int,input('Enter all the scores ').strip().split()))
grades = list()
#Method getGrade assigns grades based on grading scheme 
def getGrades(scores,grades):
    best = max(scores)
    for i in range(len(scores)): 
	    if (scores[i] >= best-10):
	        grades.append('A')
	    elif (scores[i] >= best-20):
		    grades.append('B')
	    elif (scores[i] >= best-30):
		    grades.append('C')
	    elif (scores[i] >= best-40):
		    grades.append('D')
	    else:
		    grades.append('F')
		
#Get grades
getGrades(scores, grades)
#Display results
for i in range (len(scores)):
    print("Student   {0}   score is {1}   and grade is {2}".format(i,scores[i],grades[i]))

Output:


Enter all the scores 40 55 70 58
Student   0   score is 40   and grade is C
Student   1   score is 55   and grade is B
Student   2   score is 70   and grade is A
Student   3   score is 58   and grade is B

0 0

Discussions

Post the discussion to improve the above solution.