Question: Python Question The statistics library throws an exception in many cases where the incoming dataset is empty. Also older versions of the library generated an
Python Question
The statistics library throws an exception in many cases where the incoming dataset is empty. Also older versions of the library generated an error when you calculated the mode on a set of numbers that are evenly distributed.
Import statistics as stat
integers = [1,2,3,4]
print("mode", stat.mode(integers))
The library still generates an error if the incoming data is empty:
numbers = []
print("mode", stat.mode(integers))
Also the same error would be generated if you asked for the variance of a single number:
numbers = [1]
print("variance", stat.variance(integers))
Create a function named worry_free_mode that takes a list of integers as its parameter, returns the value from statistics.mode unless there is a StatisticsError (see the docs). If there is an error, return None.
Create a function named worry_free_var that takes a list of integers as its parameter, returns the value from statistics.variance unless there is a StatisticsError (see the docs). If there is an error, return None.
must use try/except construct
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
