#๐Ÿ”’ local variable mode referenced before assignment

15 messages ยท Page 1 of 1 (latest)

night coral
mystic cosmosBOT
#

@night coral

Python help channel opened

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.

safe canopy
#
    mode = mode(number_set)
night coral
mystic cosmosBOT
#

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:
safe canopy
#

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

night coral
#

but isn't that like needing to import randint instead of just the random module? Genuine question

safe canopy
#

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
mystic cosmosBOT
#
Python help channel closed

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.