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

WHILE Loops (PYTHON) Other than the items listed above, the TA should not need t

ID: 3672101 • Letter: W

Question

WHILE Loops (PYTHON)
Other than the items listed above, the TA should not need to cover any additional programming concepts.
For this task, you will calculate the average of a set of numbers as provided by the user. Continue to prompt the user until the user enters a negative number (do not count this number in the average). Here is an example (bold underline indicates user input)

Enter number 1: 3

Enter number 2: 7

Enter number 3: 4

Enter number 4: 1

Enter number 5: -1


The average of the above 4 numbers is: 3.75

Explanation / Answer

def average():
   a = 0
   sum = 0
   count = 0
   while a >= 0 :
       print "Enter number ", (count+1)
       a = input()
       if a >= 0 :
           sum += a
           count += 1
   print "The average of the above ",count, "numbers is: ", (sum/count)

average()