Re: Python-Help ( Mean,Median & Mode)

A

Alfred Canoy

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
  • =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: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
    (e-mail address removed)

    ----- Original Message -----
    From: "Bob Gailer" <[email protected]>
    To: "Alfred Canoy" <[email protected]>; <[email protected]>;
    <[email protected]>
    Sent: Sunday, December 05, 2004 7:50 PM
    Subject: Re: [Python-Help] (fwd)
 
C

Cousin Stanley

| 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?

Alfred ....

As a method to list the numbers that have been input
and avoid having to know in advance how many numbers
are needed, you might consider a simple input mode
where a character other than an integer is input
as a stop-input-mode character ....

print '\n Input an integer at the > prompt \n'
print ' Enter a period . to end input mode'
print ' and begin processing .... \n'

prompt = '> '
period = '.'

list_input = [ ]

while True :

this_str = raw_input( prompt )

if this_str == period :

break

try :

list_input.append( int( this_str ) )

except :

print '\n Input Must be an Integer'
print ' or a period . to exit Input Mode \n'

num_entries = len( list_input )

print ' Input List .... \n'

for this_item in list_input :

print ' %s' % this_item

print '\n # Entries .... %s \n' % num_entries
 
D

Dan Bishop

Alfred Canoy said:
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
  • =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: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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,900
Latest member
Nell636132

Latest Threads

Top