Python-Help ( Mean,Median & Mode)

Dan Bishop danb_83 at yahoo.com
Mon Dec 6 05:03:51 CET 2004


"Alfred Canoy" <alred at guam.net> wrote in message news:<mailman.7180.1102250155.5135.python-list at python.org>...
> Hello,
> 
> I revised my source code. It was doing great but I'm having problem listing 
> all the numbers that I'd input. How can I input all the numbers that I 
> selected? The source code and the output below:
> 
>                                         Source code:
> 
> # compute the Mean, Median & Mode of a list of numbers:
> 
> sum = 0.0
> 
> print 'This program will take several numbers then average them'
> count = input(' How many numbers would you like to sum: ')
> current_count = 0
> freq = {}
> freq [current_count] = number
> 
> while current_count < count:
>     current_count = current_count + 1

A cleaner way of writing this is

  for current_count in xrange(1, count+1):

However, there's still a better way.

>     number = input ('Enter a number: ')

It's better to use float(raw_input('Enter a number: ')) to make sure
that the user is actually entering a number.  But there's a more
important problem with your code: when the user inputs a new number,
the old one is simply discarded, so you can't calculate the median and
mode.

>     print "Number", current_count,":",number
>     sum = sum + number
> print ' The average is:', sum/count

A better way of writing the above code is:

def input_numbers(count):
   "Asks the user for numbers, and returns them in a list."
   return [float(raw_input('Enter a number: ')) for i in
xrange(count)]

def mean(numbers):
   "Returns the arithmetic mean of a numeric list."
   return sum(numbers) / len(numbers)

numbers = input_numbers(count)
print 'The average is:', mean(numbers)

> # list the numbers selected by user then gets the median & mode
> 
> listNumbers=[]
> for list in number:
>     listNumbers[list]=listNumbers.get(x,0)+1
> 
> print listNumbers

This isn't valid code.  "number" isn't a sequence.

Also, you don't have any code for computing the median.  The simplest
way to do this is:

def median(numbers):
   "Return the median of the list of numbers."
   # Sort the list and take the middle element.
   n = len(number)
   copy = numbers[:] # So that "numbers" keeps its original order
   copy.sort()
   if n & 1:         # There is an odd number of elements
      return copy[n // 2]
   else:
      return (copy[n // 2 - 1] + copy[n // 2]) / 2

> freq = {}
> current_count(freq)=number
> while number != 0:
>     number = input ('Enter a number: ')
>     count = count + 1
>     sum = sum + number

Don't ask for numbers twice.  Just iterate over the "number" list from
earlier.

>     try:
>         freq[number] += 1
>     except KeyError:
>         freq[number] = 1
>
> max = 0
> mode = None
> for k, v in freq.iteritems():
>     if v > max:
>         max = v
>         mode = k
> print mode
> 
> 
>                                             Output:
> >>> This program will take several numbers then average them
> Number 1 : 6
> Number 2 : 9
> Number 3 : 8
> Number 4 : 4
> Number 5 : 2
>  The average is: 5.8
> Traceback (most recent call last):
>   File 
> "C:\Python24\Lib\site-packages\pythonwin\pywin\framework\scriptutils.py", 
> line 310, in RunScript
>     exec codeObject in __main__.__dict__
>   File "A:\SLP5.py", line 23, in ?
>     for x in number:
> TypeError: iteration over non-sequence



More information about the Python-list mailing list