Help with defining a function (beginner)


#1

Hi guys

I’ve created some code which works perfectly, however I need to define it which I’m not to confident with at the moment! Please can someone give me a helping hand?

Here is my code which finds the least common number in a list. I’d like to define the function LeastCommon & then include it in my code

from collections import Counter
List = [1, 3, 3, 10, 7, 2, , 7, 1, 2]
CountList = Counter(List)
Mincount = min(CountList.values())
LeastCommon = next(n for n in reversed(List) if CountList[n] == Mincount)
print(LeastCommon)

Many thanks


#2

well, defining a function is easy:

def my_function():
    # function content here

maybe you should give your function a parameter:

def my_function(the_list):
    # function content here

so you can supply a list as argument on function call

If you are able to understand tutorials doc on how to write the program you wrote, leaning function syntax shouldn’t be very challenging:

https://www.learnpython.org/en/Functions


#3

This is what I get when I try to define a function

Def LeastCommon(List):
^
SyntaxError: invalid syntax


#4

keywords (def is a keyword) are case sensitive, as you can see in the example i provided, def is all lowercase

Each language has recommendation on how to how name variable, the python recommendation is to use all lowercase for function names and variable names, if the name of variable/function consist of two words, its common to use an underscore (so least_common would then be the name of the function)

you don’t have to follow this naming convention, but its highly unusual not to.


#5

Thanks, this has helped me! I’ve managed to define my function now. However when I copy my code below the def, it fails to produce an output. However, it produces an output without the def. I’m not sure if my indents are correct, this is the bit which I get confused with & its one of the most simple parts to everyone else!


def least_common(List):
    from collections import Counter
    List = [1, 7, 3, 3, 6, 7, 2, 1, 2]
    CountList = Counter(List)
    Mincount = min(CountList.values())
    LeastCommon = next(n for n in reversed(List) if CountList[n] == Mincount)
    print(LeastCommon)

#6

Functions don’t execute unless called. So you need to call the function in order for the function to execute:

# defining function
def example():
    print "lets execute that function"

# calling function
example()

if your function has a parameter (yours has, List) you can supply an argument at function call (between the parentheses), the argument gets passed along to the function parameter

this allows you to call the function with different lists!

Did you see the learnpython link i posted:

there is a section about calling functions

List is still not following the naming convention.


#7

Thanks for you help mate! I just had a read of everything and watched a few Youtube videos and managed to get it working!


#8

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.