Source of PythonAverage.py


  1: # 
  2: # PythonAverage.py
  3: #
  4: # A sample program in the Python programming language
  5: #
  6: # Reads and calculates the average of numbers entered by the user
  7: #
  8: # Author:  Mark Young
  9: # Date:    2018-09-05
 10: #

 12: # introduce yourself
 13: print '\n'
 14: print 'Sample Program (Python)'
 15: print '======================='
 16: print 'Finds the average of positive integers entered by the user.'
 17: print 'Enter a negative number to stop input.\n'

 19: # initialize variables
 20: sum = 0
 21: count = 0

 23: # get first value
 24: num = int(input('Enter a number: '));

 26: # keep going until you get a negative number
 27: while (num >= 0):
 28:     # add to running sum
 29:     sum += num

 31:     # one more number
 32:     count += 1

 34:     # get next number
 35:     num = int(input('Enter another number or -1 to quit: '));

 37: # calculate and print average
 38: ave = float(sum) / float(count)
 39: print ('\nThe average is ' + str(ave) + '.\n')