230k views
1 vote
What does the following program do? student = 1 while student <= 3: total = 0 for score in range(1, 4): score = int(input("Enter test score: ")) total += score average = total/3 print("Student ", student, "average: ", average) student += 1

User Graziella
by
5.5k points

1 Answer

5 votes

Answer:

This program prompts for 3 score inputs and calculates the average score (of 3 Test scores) and repeats the process for 3 different students.

Step-by-step explanation:

the programming language used is python

This is how the code should naturally look like:

student = 1

while student <= 3:

total = 0

for score in range(1, 4):

score = int(input("Enter test score: "))

total += score

average = total/3

print("Student ", student, "average: ", average)

student += 1

To explain further i.e line by line

student = 1

This indicates that the scores that the program will prompt you to enter belong to the first student

while student <= 3:

This is a continous loop that breaks only when the student number exceeds 3 (it lets you know that you're iterating over this program 3 times)

total = 0

this initializes the total score to zero. In python this is how variables are initialized an and assigned a data type especially when they will be used again within the program.

for score in range(1, 4):

score = int(input("Enter test score: "))

The FOR loop is another control structure within the code. It allows you to Iterate between the range 1 - 4 ( i.e. 1,2,3) and prompts you to enter a test score 3 times.

total += score

for each time you are prompted to add a test score, the score is added to the total.

average = total/3

This line calculates the average by dividing by 3

print("Student ", student, "average: ", average)

This line prints out the student number and average score.

e.g: Student 1, average: 14

student += 1

This adds 1 to the previous students number to iterate to the next student.

Final Note: This code can be used for taking students test scores and calculating their average and can be modified further to check if a student passed of failed.

User Josephdpurcell
by
4.7k points