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:
scores = []: An empty list namedscoresis created to store the scores entered by the user.num_students = 20: A variable is set to define the number of scores to be input.- Input Loop: A
forloop runsnum_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 Trueloop withtry-exceptblock handles potentialValueErrorif the user enters non-integer input and ensures scores are within a valid range (0-100). scores.append(score)adds the valid score to thescoreslist.
- Inside the loop,
- Calculations:
total_score = sum(scores): The built-insum()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-inmax()function finds the largest score in the list.lowest_score = min(scores): The built-inmin()function finds the smallest score in the list.
- 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
More Computer Science Questions
QWrite a Python program to input 20 student scores and calculate total, average, highest, and lowest…QCalculate the average score for each student and the overall lowest score across all subjects…QCalculate the average score of three students with scores 75, 88, and 92.QWrite a Python program to assign a grade (Platinum, Gold, Silver, Bronze) based on a given score.QWrite a Python program to assign a rank based on a player's score.