On 13/09/2013 23:12, William Bryant wrote:
On Thursday, September 12, 2013 9:39:33 PM UTC+12, Oscar Benjamin wrote:
On 12 September 2013 07:04, William Bryant <gogobe...@gmail.com> wrote:

> Thanks everyone for helping but I did listen to you :3 Sorry. This is my 
code, it works, I know it's not the best way to do it and it's the long way round 
but it is one of my first programs ever and I'm happy with it:



Hi William, I'm glad you've solved your initial problem and I just

wanted to make a couple of comments about how your program could be

simplified or improved. The comments are below.


Hello, I've done this so far but why doesn't the mode function work?

'''#*************************************************************************'''
#* 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                                                        *#
'''**************************************************************************'''




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

#         |
#        |
#       \/

import time
import itertools



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

#         |
#        |
#       \/

Global variables and no parameter passing: yuck! :-)

List = []
NumberOfXItems = []
Themode = []

#-----#                   ~~Functions that I am using.~~                 #-----#

#         |
#        |
#       \/

Your function names aren't meaningful.

def HMNs():
     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:  ")

This line doesn't do anything:

     user_inputHMNs
     time.sleep(1.5)

This variable is an integer, yet it's called 'TheStr'.

     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)

Here you're comparing the list's .count method with an integer. It'll never be true!

         if List.count == int(user_inputHMNs):
             break
         mode()

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("Your list -> ", List)

The same bug as above:

         if List.count == int(user_inputHMNn):
             break
         mode()
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 mode():
     global NumberOfXItems, Themode
     for i in List:

Here you're appending an item and then the number of times that the item occurs:

         NumberOfXItems.append(i)
         NumberOfXItems.append(List.count(i))

Here you're getting the maximum entry, be it an item or the number of times an item occurs (see above). Have a look at the Counter class from the collections module:

     Themode = max(NumberOfXItems)
     print(Themode)



#-----#               ~~The functions which need calling~~               #-----#

#         |
#        |
#       \/

NOS()


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

Reply via email to