Statistics module | Sololearn: Learn to code for FREE!


Statistics module

calculating mode using the statistics module in python gives me an error when I use this line of code md = statistics.mode (sample). I can find the mean, variance, etc in a similar way without any issues. any ideas as to why this is failing?

6/12/2018 7:32:14 PM


7 Answers

New Answer


Nonduduzo That is because there is no *unique* value which is the most frequent, in your list. The mode() method does not handle such situation and raises the StatisticsError. You can handle it with a try/except clause, like print out "There is no unique mode here." for example.


Nonduduzo which Python version do you use? Perhaps it has a different implementation of the method, might be... Just for reference:


Full Gamer my code is public.


Kuba Siekierzyński I used the same set of values on my PC and it works but when I try it here on Sololearn it fails


Full Gamer import statistics sample = [1,2,3,1,4,1,5,4,3,8,4,10,3] #how come line 5 generates an error? md = statistics.mode(sample) med = statistics.median(sample) var = statistics.variance(sample) dev = statistics.stdev(sample) print("Median = ",med) print("Variance = ",round(var)) print("Standard deviation = ",round(dev))


I am using python 3