Python-Help ( Mean,Median & Mode)

Alfred Canoy alred at guam.net
Sun Dec 5 07:35:45 EST 2004


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
    number = input ('Enter a number: ')
    print "Number", current_count,":",number
    sum = sum + number
print ' The average is:', sum/count

# 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

freq = {}
current_count(freq)=number
while number != 0:
    number = input ('Enter a number: ')
    count = count + 1
    sum = sum + number
    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


Thank you much!
Al
 _ _
_ _
Alfred Canoy
Agana, Guam
Pacific time
alred at guam.net

----- Original Message ----- 
From: "Bob Gailer" <bgailer at alum.rpi.edu>
To: "Alfred Canoy" <alred at guam.net>; <python-help at python.org>; 
<python-list at python.org>
Sent: Sunday, December 05, 2004 7:50 PM
Subject: Re: [Python-Help] (fwd)


> At 10:07 PM 12/4/2004, Alfred Canoy wrote:
>>Hello,
>>
>>I'm just new to programming and would like to ask for help..
>>
>>Build a module that contains three functions that do the following:
>>
>>      a.. Compute the average of a list of numbers
>>      b.. Finds the statistical median value of a list of numbers
>
> The middle value in a distribution, above and below which lie an equal 
> number of values.
>
>>      c.. Finds the mode of a list of numbers
>
> The value or item occurring most frequently in a series of observations or 
> statistical data.
>
>>Can you please give me clue how I should start solving the following 
>>problem
>>below? Here's the source code that I did so far:
>>
>># compute the average of a list of numbers:
>># Keeps asking for numbers until 0 is entered
>># Prints the average value
>>
>>count = 0
>>sum = 0
>>number = 1
>>
>>print 'Enter 0 to exit the loop'
>>while number != 0:
>>    number = input ('Enter a number: ')
>>    count = count + 1
>>    sum = sum + number
>>count = count -1
>>print ' The average is:', sum/count
>
> Great start. In addition append each numbers to a list. Then it is easy to 
> find the middle of the list for the median. Use a dictionary keyed by the 
> numbers to count their frequency, then find the entry with the highest 
> frequency.
>
> See http://www.honors.montana.edu/~jjc/easytut/easytut/ for lists and 
> dictionaries.
>
> Give it a shot, show us what you come up with, and we'll take the next 
> step.
>
> Bob Gailer
> bgailer at alum.rpi.edu
> 303 442 2625 home
> 720 938 2625 cell 




More information about the Python-list mailing list