We read your photo as:

Write a Python program to calculate and display the total, average, highest, and lowest scores for 20 students.

Computer Science

ScanSolve AI Answer

Here is a Python program that fulfills the requirements:

# Initialize an empty list to store the scores
scores = []
num_students = 20

print("Please enter 20 student scores:")

# Step 1: Input 20 student scores
for i in range(num_students):
    while True:
        try:
            score = int(input(f"Enter score for student {i + 1}: "))
            if 0 <= score <= 100: # Assuming scores are between 0 and 100
                scores.append(score)
                break
            else:
                print("Score must be between 0 and 100. Please try again.")
        except ValueError:
            print("Invalid input. Please enter an integer score.")

# Step 2: Calculate Total, Average, Highest, and Lowest scores
total_score = sum(scores)
average_score = total_score / num_students
highest_score = max(scores)
lowest_score = min(scores)

# Step 3: Display the results
print("\n--- Student Score Analysis ---")
print(f"Total number of students: {num_students}")
print(f"Total score: {total_score}")
print(f"Average score: {average_score:.2f}") # Format to 2 decimal places
print(f"Highest score: {highest_score}")
print(f"Lowest score: {lowest_score}")

Explanation:

  1. scores = []: An empty list named scores is created to store the scores entered by the user.
  2. num_students = 20: A variable is set to define the number of scores to be input.
  3. Input Loop: A for loop runs num_students (20) times.
    • Inside the loop, input() prompts the user to enter a score for each student.
    • int() converts the input string to an integer.
    • A while True loop with try-except block handles potential ValueError if the user enters non-integer input and ensures scores are within a valid range (0-100).
    • scores.append(score) adds the valid score to the scores list.
  4. Calculations:
    • total_score = sum(scores): The built-in sum() function calculates the sum of all scores in the list.
    • average_score = total_score / num_students: The average is calculated by dividing the total score by the number of students.
    • highest_score = max(scores): The built-in max() function finds the largest score in the list.
    • lowest_score = min(scores): The built-in min() function finds the smallest score in the list.
  5. Display Results: Finally, print() statements are used to display all the calculated values in a clear format. The average score is formatted to two decimal places using an f-string :.2f.
Was this helpful?

Still stuck on this one?

Ask a follow-up, and the answer starts from this question.

Got a different question?Ask your own question