Advertisement
El3ctr0G33k

Untitled

Oct 25th, 2014
852
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.26 KB | None | 0 0
  1. """
  2. Goal
  3. Take the users input (A series of numbers)
  4. Calculate the Mean (Sum of all numbers divided by the amount of numbers)
  5. Calculate the Mode (The number that occurs the most)
  6. Calculate the median (The middle number when sorted in ascending order)
  7. Output these averages
  8. """
  9.  
  10. ## take input and add to a list
  11. numbers = []
  12. while len(numbers) < 5:
  13. numbers.append(raw_input("Please give a number"))
  14.  
  15. print numbers
  16. ## calculate mean
  17.  
  18. def calculate_mean(numbers):
  19. total = float(0)
  20. for n in numbers:
  21. total += int(n)
  22. return (total/len(numbers))
  23.  
  24. print calculate_mean(numbers)
  25.  
  26. ## calculate mode
  27.  
  28. def calculate_mode(numbers):
  29. highcount = 0
  30. for n in numbers:
  31. if numbers.count(n) > numbers.count(highcount):
  32. highcount = n
  33. return highcount
  34.  
  35. print calculate_mode(numbers)
  36.  
  37. ## calculate median
  38.  
  39. def median(numbers):
  40. sortednumbers = sorted(numbers)
  41. # print sortednumbers
  42. listlength = len(sortednumbers)
  43. if listlength == float(1):
  44. return sortednumbers[listlength-1]
  45. elif listlength % 2 != 0:
  46. return float(sortednumbers[listlength/2])
  47. else:
  48. return float(sortednumbers[listlength/2] + sortednumbers[(listlength/2)-1]) / 2.0
  49.  
  50. print median(numbers)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement