How do I calculate a mean with python?

Joel Goldstick joel.goldstick at gmail.com
Tue Sep 17 17:02:16 EDT 2013


On Tue, Sep 17, 2013 at 4:10 PM, William Bryant <gogobebe2 at gmail.com> wrote:

> Ok I think I've fixed it thanks I read everything.
>
>
> '''**************************************************************************'''
> #* Name:        Mode-Median-Mean Calculator
>     *#
> #*
>    *#
> #* Purpose:     To calculate the mode, median and mean of a list of
> numbers   *#
> #*              and the mode of a list of strings because that is what we
> are *#
> #*              learning in math atm in school :P
>     *#
> #*
>    *#
> #* Author:      William Bryant
>    *#
> #*
>    *#
> #* Created:     11/09/2013
>    *#
> #*
>    *#
> #* Copyright:   (c) William 2013
>    *#
> #*
>    *#
> #* Licence:     IDK :3
>    *#
>
> '''**************************************************************************'''
>
> The above comments are a mess.  In python, use docstrings -- put 3 quotes
> in front of all the documentation, and 3 at the end like:
> """
>
    Name:        Mode-Median-Mean Calculator
    Purpose:     To calculate the mode, median and mean of a list of
numbers   *#
    """

There is a utility called pydoc that will pull all docstrings from a module
and produce nice documentation.  So better to learn this style early

#-----#                   ~~Import things I am using~~
#-----#

#         |
#        |
#       \/

import time
import itertools

I don't think you use itertools, so remove the reference

#-----#        ~~Variables that I am using, including the list.~~
 #-----#

#         |
#        |
#       \/

List = []
NumberOfXItems = []

Using global variables is a very bad idea.  Since you seem to be a novice,
I'm wondering where you even learned about global variables.  If your
instructor taught you, then (s)he should look for a new line of work.
Using globals promotes lazy thought, makes debugging impossible in any
reasonably large piece of code.  Google about that.  Its important


#-----#                   ~~Functions that I am using.~~
> #-----#
>
> #         |
> #        |
> #       \/
>
> Use better names.  What does HMNs mean?  This function asks the user to
input a list of strings or numbers.  It is almost identical to HMNn to that
point that they should be combined most likely.


> def HMNs():
>
      """
      Here you should write about the function -- what it does, what is
needs passed to it, and what it returns.
      """




>     global TheStr, user_inputHMNs, List_input, List
>     user_inputHMNs = input("You picked string. This program cannot
> calculate the mean or median, but it can calculate the mode. :D  How many
> strings are you using in your list? (Can not be a decimal number)  \nEnter:
>  ")
>     user_inputHMNs
>     time.sleep(1.5)
>     TheStr = int(user_inputHMNs)
>     for i in range(TheStr):
>         List_input = input("Enter your strings. (One in each input field):
>  ")
>         List.append(List_input)
>         print("Your list -> ", List)
>         if List.count == int(user_inputHMNs):
>             break
>         print("\n*Mode*:", mode())
>         print("*Median*:",  "<Coming soon!>\n")
>         print("*Mean*:",    mean())
>
> def HMNn():
>     global TheNum, user_inputHMNn, List_input, List
>     user_inputHMNn = input("You picked number. :D How many numbers are you
> using in your list? (Can not be a decimal number) \nEnter:  ")
>     user_inputHMNn
>     time.sleep(1.5)
>     TheNum = int(user_inputHMNn)
>     for i in range(TheNum):
>         List_input = input("Enter your numbers. (One in each input field):
>  ")
>         List_input = int(List_input)
>         List.append(List_input)
>         print("\nYour list -> ", List)
>         if List.count == int(user_inputHMNn):
>             break
>         print("\n*Mode*:", mode())
>         print("*Median*:",  "<Coming soon!>")
>         print("*Mean*:",    mean())
>
> def NOS():
>     while True: # Loops forever (until the break)
>         answer = input("Does your list contain a number or a string?
>  \nEnter: ")
>         answer = answer.lower()
>         if answer in ("string", "str", "s"):
>             HMNs()
>             break
>         elif answer in ("number", "num", "n", "int"):
>             HMNn()
>             break
>         elif answer in ("quit", "q"):
>             break  # Exits the while loop
>         else:
>             print("You did not enter a valid field, :P Sorry.  \nEnter: ")
>     time.sleep(1.5)
>
> def mean():
>     thesum = sum(List)
>     amount = len(List)
>     themean = thesum / amount
>     return themean
>
> def mode():
>     max_occurrences = 0
>     themode = None
>     for i in List:
>         thecount = List.count(i)
>         if thecount > max_occurrences:
>             max_occurrences = thecount
>             themode = i
>     return themode
>
>
>
> #-----#               ~~The functions which need calling~~
> #-----#
>
> #         |
> #        |
> #       \/
>
> NOS()
>


So, I just added a few criticisms earlier above.  To sumarize:
   avoid global variables, document functions with docstrings, use really
clear names for variables including function  names.  If you have code in
two functions that is almost identical, figure out how to make them one
function

good luck

> --
> https://mail.python.org/mailman/listinfo/python-list
>



-- 
Joel Goldstick
http://joelgoldstick.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20130917/a2c071df/attachment.html>


More information about the Python-list mailing list