This question is for the code challenge where you are asked to calculate the mean and the mode of a list of given integers. https://www.codecademy.com/code-challenges/code-challenge-calculate-the-mean-and-mode-python
I don’t understand why none of the test cases are passing even though I get the right output from the sample list.
The sort in the beginning is because the question asks you to return the mode with the least value, and sorting it before finding the mode eliminates any extra steps for finding the lower of the modes. At least, in theory.
def stats_finder(array): # Write your code here array.sort() total = 0 count = 0 for num in array: total += num count += 1 mean = total / count mode_count = 0 mode = 0 for num in array: if array.count(num) > mode_count and num != mode: mode_count = array.count(num) mode = num return mean, mode print(stats_finder([500, 400, 400, 375, 300, 350, 325, 300]))