67.9k views
0 votes
Python Lists and Events: 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.

User Elad Benda
by
4.5k points

1 Answer

3 votes

Answer:

import random

scores = []

for i in range(8):

score = random.randint(0,100)

scores.append(score)

best_score = max(scores)

letter_grade = ""

for s in scores:

if s >= best_score - 10:

letter_grade = "A"

elif s >= best_score - 20:

letter_grade = "B"

elif s >= best_score - 30:

letter_grade = "C"

elif s >= best_score - 40:

letter_grade = "D"

else:

letter_grade = "F"

print(letter_grade)

Step-by-step explanation:

Import the random module

Create an empty list to hold the scores

Create a for loop that iterates 8 times. Inside the loop, create 8 random scores and put them in the scores

When the loop is done, find the best_score using max method

Create another for loop that iterates through the scores. Check each score and set the letter_grade depending on the given conditions.

User Fruchtzwerg
by
4.6k points