1

I am writing a program to find the mean, median and mode of a list. Rather than doing all of this manually, I am using Python's "Statistics" module, which has built in functions for each of these.

However I found a problem, when there is no mode (that is, when there is more than one answer for the mode (e.g., when the list = [1,3,5,7,10])) the module throws an error:

statistics.StatisticsError: no unique mode; found 5 equally common values

Im not sure how I would specifically use an except statement to be executed when there is an error like this, I think I can do something like

try:
    print("The Mode = ",statistics.mode(userNumbers))
except statistics.StatisticsError
    print("There was an error with the statistics module")

To show that there was a problem with the statistics module, however when I try

try:
    print("The Mode = ",statistics.mode(userNumbers),")
except statistics.StatisticsError: no unique mode; found 5 equally common value:
    print("There is no single mode")

Python thinks that I am trying to end the line at the colon after Error, I tried using a backslash ("\") to skip the colon but that didnt work, and im not sure how else I would specifiy the error that was given off without ignoring that character.

Is there a way to ignore that colon or would I have to do something else altogether?

Thanks to anyone that atleast looks at my problem <3

Cettt
  • 11,460
  • 7
  • 35
  • 58
  • Possible duplicate of [Python: Catching specific exception](https://stackoverflow.com/questions/13531247/python-catching-specific-exception) – yinnonsanders Oct 27 '17 at 16:09
  • I cant quite understand what is going on in that problem :c, it does look very similar however –  Oct 27 '17 at 16:12
  • To just catch the exception, just use `except statistics.StatisticsError:` (note: with a colon at the end), but if you want to restrict it to that particular error, you can use the answers in the other question. – yinnonsanders Oct 27 '17 at 16:24

1 Answers1

0

Try this.This will give you the exact detailed error which you want i.e

userNumbers = [1,3,5,7,10]

def mode(list1):
    try:
        print("The Mode = ",statistics.mode(userNumbers))
    except statistics.StatisticsError as e:
        print("There was an error with the statistics module")
        print e

mode(userNumbers)

Output

C:\Python27\python.exe 
C:/Users/kapil/PycharmProjects/KapilsucksatPython/py/O.py
There was an error with the statistics module
no unique mode; found 5 equally common values

Process finished with exit code 0

Note the usage of raising the exception as a alias "e" here and then printing e which prints the exact problem that lies with the module statistics

nomoreabond2017
  • 150
  • 2
  • 5
  • Thank you, this is perfect, I do have a question however: I have seen in other answers they also use "e", this sounds weird me saying this but by using "e" as the variable name does it show the exact detailed error? Or could I for example, do "except statistics.StatisticsError as i:"? –  Oct 27 '17 at 19:00
  • Go ahead plz but dont forget to accept my answer :) !! – nomoreabond2017 Oct 27 '17 at 19:01
  • Doesnt matter what you use.. "e" or "i" As i mentioned, it just an alias for the exception that you expect the function to raise and capture it using that alias so that you can print it further. Just replace e with i in my answer and see for yourself :) – nomoreabond2017 Oct 27 '17 at 19:05