The most common answer for the 5.2.8 Average Test Score CodeHS is:
Score = 0
for For3Scores in range(1,4):
Average = int(input())
Score += Average
print((Score/3))
This code for calculating the average test score is almost correct, but there are a couple of improvements to be made for better clarity and accuracy:
- The print statement for the average should be placed outside the loop, so it only prints once after all scores are entered and summed.
- It’s good practice to use descriptive variable names. Instead of
For3Scores
andAverage
, names likescore
andtotal_score
might be more appropriate.
Here’s the revised version of the code:
total_score = 0
for _ in range(3): # Loop 3 times for 3 scores
score = int(input("Enter score: ")) # Get the score from the user
total_score += score # Add the score to the total
average_score = total_score / 3 # Calculate the average
print("Average score:", average_score) # Print the average
In this revised code:
- The loop iterates three times, each time asking for a score and adding it to
total_score
. - The average is calculated as
total_score / 3
and is printed after the loop completes. This ensures the average is only displayed once, after all the scores are entered. - Descriptive variable names are used for better readability.