Question

create a program that calculates a students average score and assigns a letter grade. The functions must:
1. Accept a list of scores as input
2. Calculate the average score
3. Assign a letter grade based on the following scale:
90 - 100 = A
80 - 89 = B
70 - 79 = C
60 - 69 = D
Below 6 = F
4. Return the average score and the letter grade
step 2
write out the function design using pseudocode before implementing it in Python. Ex:
Function: calculate_grade (scores)
1. Compute the sum of all scores
2. Divide by the total number of scores to get the average
3. Use conditional statements to determine the letter grade
4. Return both the average and letter grade

Answers

GPT-4o mini
Here's the function design using pseudocode, followed by the implementation in Python.

### Pseudocode

```
Function: calculate_grade(scores)
1. If the scores list is empty
a. Return "No scores provided"

2. Initialize total_score to 0
3. For each score in scores
a. Add score to total_score

4. Compute the average as total_score divided by the length of scores

5. If average is greater than or equal to 90
a. Set letter_grade to "A"
6. Else if average is greater than or equal to 80
a. Set letter_grade to "B"
7. Else if average is greater than or equal to 70
a. Set letter_grade to "C"
8. Else if average is greater than or equal to 60
a. Set letter_grade to "D"
9. Else
a. Set letter_grade to "F"

10. Return average and letter_grade
```

### Python Implementation

```python
def calculate_grade(scores):
# Step 1: Check if the scores list is empty
if not scores:
return "No scores provided"

# Step 2: Initialize total_score to 0
total_score = 0

# Step 3: Sum all scores
for score in scores:
total_score += score

# Step 4: Calculate the average
average = total_score / len(scores)

# Step 5: Determine the letter grade
if average >= 90:
letter_grade = "A"
elif average >= 80:
letter_grade = "B"
elif average >= 70:
letter_grade = "C"
elif average >= 60:
letter_grade = "D"
else:
letter_grade = "F"

# Step 6: Return both average and letter grade
return average, letter_grade

# Example usage:
scores = [88, 92, 79, 85, 76]
average, grade = calculate_grade(scores)
print(f"Average Score: {average}, Letter Grade: {grade}")
```

This Python program implements the design documented in the pseudocode. It accepts a list of scores, calculates the average score, assigns a corresponding letter grade, and returns both the average score and grade. The example usage demonstrates how the function can be used to process a list of scores.