# Hack your homework: Mean, Median and Mode data = [] # Creates an empty list to store the numbers in print("This program will make mean, median and mode very easy!") print("\nFirst, we need to enter some numbers!") num=input("Type your first number: ") while num: data.append(float(num)) #Converts num into a float so we can do division print("\n",data) num = input("Add another number, or press enter to move on.") #Work out the mean total = sum(data) length = len(data) print("\n\nThe Mean Is:",total/length) #Puts the numbers in order data.sort() print("\n\nHere's the list in numerical order:\n", data) #Work out the median oddness = length%2 half= length//2 if oddness == 1: print("The median is:",data[half]) else: low = float(data[half-1]) high = float(data[half]) print("The median is half way between", low, "and", high) print("That makes it:",low+(high-low)/2) #Work out the mode #I'm using a list again hits = [] for item in data: tally = data.count(item) #Makes a tuple that is the number of huts paired with the relevant number values = (tally, item) # Only add one entry for each number in the set if values not in hits: hits.append(values) hits.sort(reverse=True) if hits[0][0]>hits[1][0]: print("\n\nThe mode is:", hits[0][1], "hit appeared", hits[0][0], "times.") else: print("There is not a mode")
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question