219k views
0 votes
Python: Write a program that generates 8 random scores (between 0 and 100), store them in an array,

finds the best score, and then assigns grades based on the following scheme (use loops
wherever possible):
Grade is A if score >= best – 10
Grade is B if score >= best – 20
Grade is C if score >= best – 30
Grade is D if score >= best – 40
Grade is F otherwise.

1 Answer

2 votes

Answer:

The program written in python is as follows;

Note that the program makes use of function named checkbest

Lines written bold are comments and are used to replace the explanation section

#Program starts here

#This line imports the random module into the program

import random

#This line defines the function checkbest, with 2 parameters

def checkbest(score,best):

#The following if condition implements the condition as stated in the

#program requirement

if score >= best- 10:

grade = "A"

elif score >= best - 20:

grade = "B"

elif score>= best - 30:

grade = "C"

elif score >= best - 40:

grade = "D"

else:

grade = "F"

#This line returns the letter grade depending on the above

#conditions

return "Grade: "+grade

#The main method starts here

#This line declares an empty list

array = []

#This line iterates from 1 to 8

for i in range(1,9):

#This line generates a random integer between 0 and 100 (inclusive)

score = random.randint(0,100)

#This line inserts the generated score in the list

array.append(score)

#This line sorts the list in ascending order

array.sort()

#This line gets the best score

best = array[7]

#This line iterates through the elements of the list

for i in range(0,8):

#This line prints the current score

print("Score: "+str(array[i]))

#This line calls the function to print the corresponding letter grade

print(checkbest(array[i], best))

#This line prints an empty line

print(" ")

#The program ends here

User Freestate
by
6.4k points