Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

USING PYTHON Create an application that uses a while loop to collect test scores

ID: 3577456 • Letter: U

Question

USING PYTHON

Create an application that uses a while loop to collect test scores and calculate their average You will need an accumulator variable, which works like this: testScore testSum testSum = testSum + testScore or testSum + testScore You will need a counter to calculate the average You will need a method to stop your loop Suggestions: Exit the loop when you detect a negative number Exit the loop when you detect a letter Exit the loop when you detect a specific word Don't forget to use comments Write good output Check your work with these scores: 89, 93, 79 Average is 87.0

Explanation / Answer

import sys
sys.tracebacklimit=0
count = 0
testSum = 0
print "Enter test scores in a loop , input -1 to stop entering"

while 1:
try:
testScore = input()
if int(testScore) < 0 :
print "Quit as -1 entered "
break
  
else:
testSum += int(testScore)
count+=1
except valueError:
print("Please enter only positive numbers for test scores")

except NameError:
if type(testScore ) == char:
print("Please enter only positive numbers for test scores")

if testScore == 'Q':
print "Quit as Q is entered "
break
  


if count > 0:   
Average = testSum/float(count)   
print "Average test score = ", Average
else:
print "Count is less than zero"

-------------------------------------------------------

output

Enter test scores in a loop , input -1 to stop entering                                                                                                                         

89                                                                                                                                                                              

93                                                                                                                                                                              

79                                                                                                                                                                              

-1                                                                                                                                                                              

Quit as -1 entered                                                                                                                                                              

Average test score =  87.0