Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   Python (http://www.programmingforums.org/forum43.html)
-   -   Coding this function (http://www.programmingforums.org/showthread.php?t=11977)

Macdonalds Nov 23rd, 2006 2:38 PM

Coding this function
 
Does anyone know how to code this function? :)

An average function, that when the function is coded, it returns the average of the integers typed in a list:
for example:
mean(listofinteger)
>>> mean([4,5,6])
5
>>> mean([7,8,9])
8

etc

Arevos Nov 23rd, 2006 2:46 PM

Sounds like homework. :rolleyes:

Macdonalds Nov 23rd, 2006 2:47 PM

Yea. =(

DaWei Nov 23rd, 2006 3:40 PM

Imagine how you would discover the mean if you were doing it mentally and write Python code to do the same thing. If it doesn't work for you, show your code and ask for help. Incidentally, the mean that you indicate that you want is not the average.

Arevos Nov 23rd, 2006 4:49 PM

Quote:

Originally Posted by DaWei (Post 119584)
Incidentally, the mean that you indicate that you want is not the average.

:confused: Seems like the average to me.

DaWei Nov 23rd, 2006 6:08 PM

You are correct, of course, too much turkey and Texas Hold 'Em fried my brain again.

purpleorange Nov 23rd, 2006 6:34 PM

Seems like no one wants to answer your question. So will be the first. =) Yes, I know how to code a function that does that.

Since you show the ability to find the average of numbers yourself, I assume that the previous people that replied were correct in saying that this is a homework problem.

Here are some clues if you are still stuck:
1. The average, as you probably know, is the sum of all the elements of the list divided by the length of the list.
2. A way of accessing the elements in the list is by using a 'for' loop.
3. The len() method will return the length of the list.

Sane Nov 23rd, 2006 7:32 PM

It might be "cheating", but there is the "sum" function.

:

>>> sum([4,5,6])
15
>>> sum([7,8,9])
24


Dietrich Nov 26th, 2006 12:38 PM

It might be "cheating", but there is the "len" function.
:

>>> len([7,8,9])
3

Homework? I can't believe that there are actually schools progressive enough to teach Python. I thought most of those institutions of higher learning were stuck with Java.

public2 Nov 27th, 2006 2:19 AM

Quote:

Originally Posted by Dietrich (Post 119751)
It might be "cheating", but there is the "len" function.
:

>>> len([7,8,9])
3

Homework? I can't believe that there are actually schools progressive enough to teach Python. I thought most of those institutions of higher learning were stuck with Java.

We're learning Python AND Java, and the integration between them, but in this first semester, we just learned Python. Baby steps :-)


All times are GMT -5. The time now is 8:22 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC