In python, write a program that reads a list of scores and then assigns grades H
ID: 3807165 • Letter: I
Question
In python, write a program that reads a list of scores and then assigns grades
Have your program:
Use 100 for the best score
Begin the program and each function with a descriptive comment.
Define main( ) to call the two functions described below.
Define a function to prompt the user to enter valid scores until they enter a sentinel value -999. Have this function both create and return a list of these scores. Do not store -999 in the list!
Have main( ) then pass this the list to a second function which traverses the list of scores printing them along with their appropriate grade.
Run your program with a score within each of the possible grade ranges. See the sample output below.
Use local variables, rather than global.
Use structured code (e.g., no unstructured exits from loops).
Sample output:
94 is an A
83 is a B
72 is a C
61 is a D
59 is an F
Explanation / Answer
#python code
def findGrades(scoreList):
# repeat the loop for all scores in the list
i = 0
while i < len(scoreList):
# get current score
score = int(scoreList[i])
# findd and print the grade of the student
if score >= 90:
print "Student", (i+1) , "score is", score, "and grade is A"
elif score >= 80:
print "Student", (i+1) , "score is", score, "and grade is B"
elif score >= 70:
print "Student", (i+1) , "score is", score, "and grade is C"
elif score >= 60:
print "Student", (i+1) , "score is", score, "and grade is D"
else:
print "Student", (i+1) , "score is", score, "and grade is F"
i += 1
def validScore():
scoreList = []
while True:
score = int(raw_input("Enter a score: "))
if score == -999:
break
else:
scoreList.append(score)
return scoreList
# main() function
def main():
scoreList = validScore()
findGrades(scoreList)
main()
'''
output:
Enter a score: 94
Enter a score: 83
Enter a score: 72
Enter a score: 61
Enter a score: 59
Enter a score: -999
Student 1 score is 94 and grade is A
Student 2 score is 83 and grade is B
Student 3 score is 72 and grade is C
Student 4 score is 61 and grade is D
Student 5 score is 59 and grade is F
'''
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.