#๐ local variable mode referenced before assignment
15 messages ยท Page 1 of 1 (latest)
@night coral
Remember to:
- Ask your Python question, not if you can ask or if there's an expert who can help.
- Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
- Explain what you expect to happen and what actually happens.
:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.
What is mode()?
mode = mode(number_set)
according to a stack overflow thread its a function of statistics and I don't get any import errors so I'm assuming I have it
!d statistics.mode
statistics.mode(data)```
Return the single most common data point from discrete or nominal *data*. The mode (when it exists) is the most typical value and serves as a measure of central location.
If there are multiple modes with the same frequency, returns the first one encountered in the *data*. If the smallest or largest of those is desired instead, use `min(multimode(data))` or `max(multimode(data))`. If the input *data* is empty, [`StatisticsError`](https://docs.python.org/3/library/statistics.html#statistics.StatisticsError) is raised.
`mode` assumes discrete data and returns a single value. This is the standard treatment of the mode as commonly taught in schools:
```py
>>> mode([1, 1, 2, 3, 3, 3, 3, 4])
3
``` The mode is unique in that it is the only statistic in this package that also applies to nominal (non\-numeric) data:
You didn't import mode, you imported statistics
So if you wanna use it, use statistics.mode()
Else change the import to from statistics import mode
but isn't that like needing to import randint instead of just the random module? Genuine question
randint is from random module. But Python doesn't know or see that. So you have to tell it to get it from there
- import randint
+ from random import randint
This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.