Question
Python Lesson Assignment The statistics library throws an exception in many cases where the incoming dataset is empty. Also older versions of the library generated
Python Lesson Assignment
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 stats numbers = [1,2,3,4] print("mode", stats.mode(numbers))
The library still generates an error if the incoming data is empty:
numbers = [] print("mode", stats.mode(numbers))
Also the same error would be generated if you asked for the variance of a single number:
numbers = [1] print("var", stats.variance(numbers))
Create a function named worry_free_mode that takes a list of numbers 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 numbers as its parameter, returns the value from statistics.variance unless there is a StatisticsError (see the docs). If there is an error, return None.
-
You must use try/except construct
-
You may need to refer to https://docs.python.org/3/library/statistics.html
-
You must put your code inside the module lesson.py
-
Write a test in main.py that uses worry_free_mode that confirms your implementation
-
If you are unsure how to reference or use StatisticsError from the statistics module, see the previous lesson on modules.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started