#๐Ÿ”’ calculate mode without using libraries

23 messages ยท Page 1 of 1 (latest)

crystal kindle
#

the assignment is to create a function that calculates mode without using any built in libraries, and for some reason, mine doesn't print anything. no error messages or anything.

def find_mode(list):
    counts = {}
    for item in list:
        if item in counts:
            counts[item] += 1
        else:
            counts[item] = 1
    theMode = max(counts.values())
    return theMode
    
def main():
    data = [1, 2, 2, 3, 3, 3, 4]
    listMode = find_mode(data)
    print("Mode:", listMode)```
zinc crownBOT
#

@crystal kindle

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.

forest verge
crystal kindle
#

is this better?

def find_mode(lst):
    counts = {}
    for item in lst:
        if item in counts.keys():
            counts[item] += 1
        else:
            counts[item] = 1
    theMode = max(counts.values())
    for key in counts:
        if key == theMode:
            theMode = key
    return theMode
    
def main():
    data = [1, 2, 2, 3, 3, 3, 4]
    mode = find_mode(data)
    print("Mode:", mode)```
tiny estuary
#

those shouldn't cause issues, i agree calling a variable list isn't ideal but it should work as-is

#

@crystal kindle all the functionality is in the main() function, is it ever called?

crystal kindle
#

wdym (sorry i'm a beginner)

tiny estuary
#

ok so in the main() function, you call find_mode(data), which calls the find_mode() function with a list of data

#

find_mode() never runs until you call it, and you do inside main()

#

similarly, main() won't run until you call it

#

if what you posted here is your entire script, then it will never do anything

crystal kindle
#

ok yeah that's the problem then one sec

tiny estuary
#

do you know about if __name__ == '__main__'?

crystal kindle
#

no, but just typing main() fixed it

tiny estuary
#

you want to put this at the bottom of your script:

if __name__ == '__main__':
  main()
#

it doesn't make much difference for a single script, but it's good practice in situations like this

#

eventually you'll get to a point where you might write and import modules with the import statement, and this pattern prevents the script from being executed as soon as it's imported

warm storm
#
[1, 2, 2, 3, 3, 4]```What should be the mode of this collection? 2, 3 or 2.5, or (2, 3)
forest verge
forest verge
crystal kindle
#

alright i figured it out thanks

zinc crownBOT
#
Python help channel closed using Discord native close action

This help channel has been closed. 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.