Assign sum_extra with the total extra credit received given list test_grades. Full credit is 100, so anything over 100 is extra credit. For the given program, sum_extra is 8 because 1 0 7 0 is 8. Sample output for the given program with input: '101 83 107 90'

Respuesta :

Answer:

Here is the Python program:

test_grades = [101, 83, 107, 90]      # list of test grades

sum_extra = 0  # stores total sum of extra credit

for i in test_grades:  #loop to check extra credit in test grades

   if i > 100:  # if grade is greater than 100

       i = i - 100  # 100 is subtracted from grade

   else:  # otherwise

       i = 0  

   sum_extra = sum_extra + i #adds i to to sum_extra

print('Sum extra is:', sum_extra) # prints the value of sum_extra

Explanation:

This program assigns sum_extra with total extra credit received in list test_grades. Lets see how this program works step wise.

We have a list with the following elements 101, 83, 107 and 90

sum_extra is initialized to 0

For loop has variable i that works like an index and moves through the list of test_grades

First i is positioned at 101. If condition in the loop checks if the element at i-th position is greater than 100. It is true because 101 > 100. So 100 is subtracted from 101 and i becomes 1. So this statement is executed next sum_extra = sum_extra + i

                  =  0 + 1

So the value of sum_extra = 1

Next i is positioned at 83. If condition in the loop checks if the element at i-th position is greater than 100. It is false to i become 0.So this statement is executed next sum_extra = sum_extra + i

                  =  1 + 0

So the value of sum_extra = 1

Next i is positioned at 107. If condition in the loop checks if the element at i-th position is greater than 100. It is true because 107 > 100. So 100 is subtracted from 107 and it becomes 7. So this statement is executed next sum_extra = sum_extra + i

                  =  1 + 7

So the value of sum_extra = 8

Next i is positioned at 90. If condition in the loop checks if the element at i-th position is greater than 100. It is false to i become 0.So this statement is executed next sum_extra = sum_extra + i

                  =  8 + 0

So the value of sum_extra = 8

So the output of the above program is 8.

Output:

Sum extra is: 8

Ver imagen mahamnasir