dylan stetts
dylan stetts

Reputation: 11

Why am I getting "Local variable unused in function" warning?

I'm new to programming and have just hit my first speed bump since starting this semester. We have been working in python and our latest assignment has us altering a previous program to use functions. I am understanding how to use them and what not, but some small things with local variables I think I lack in some conceptual understanding.

I use pycharm to write my assignments, and I can see it states one of my variables is unused and I don't understand why or how to fix it. I've been tinkering for a couple hours and am lost.

# Function "checkName" takes in 2 parameters
# The first is name, which will be given by user input
# The second is nameList, which comes from the function getNamesList
def checkName(name, nameList):
    for i in range(0, len(nameList)):
        if name == nameList[i]:
            rank = i + 1
            break
        else:
            rank = 0 ## This rank is undefined ##
        return rank

Any pointers on what I'm doing wrong? An explanation of the differences between defining local and global variables would be appreciated as well!

Upvotes: 0

Views: 3415

Answers (6)

tgikal
tgikal

Reputation: 1680

Probably the shortest it can be:

nameList = ["a", "b", "c"]

def checkName(name, nameList):
    return(nameList.index(name) + 1 if name in nameList else 0)

for i in ["a", "b", "c", "d"]:
    print(checkName(i, nameList))

Result:

1
2
3
0

Upvotes: 0

dylan stetts
dylan stetts

Reputation: 11

Thank you all very much for the quick and helpful replies! My indentation was off, silly error. Honestly don't know how I overlooked it so many times.

I also appreciate the distinction on local variables. I think my previous understanding was alright, but the pycharm note i was getting threw me for a loop.

Upvotes: 0

Mats Wichmann
Mats Wichmann

Reputation: 909

From the point of view of the code as written, rank is defined but not used - you set it and break, so it looks like nothing is done with it. Once again, it's that indent others have mentioned.

Upvotes: 0

rockikz
rockikz

Reputation: 646

By reading your code, I understand that this functions returns the rank or the index of a name in a given list of names. You have a wrong indentation on the return statement.

So to improve your code, check this:

def checkName(name, nameList):
    for i in range(nameList):
        if name == nameList[i]:
            return i + 1
        # you don't need an else here, just return -1 or 0 or None ( by default )
        # whenever `name` doesn't exists on the nameList
    # not very pythonic
    return 0

Your code was not always using the local variable rank because of the wrong indentation.

Upvotes: 0

Bill the Lizard
Bill the Lizard

Reputation: 405775

You get the error "Local variable 'rank' value not used" on the line rank = i + 1 because the the break statement on the next line causes the the function to end without ever reading the value from rank. This is only the case because your return statement is indented too far. Move it back one level of indentation so it doesn't return until the loop is done and the warning will go away.

def checkName(name, nameList):
    for i in range(0, len(nameList)):
        if name == nameList[i]:
            rank = i + 1
            break
        else:
            rank = 0 ## This rank is undefined ##
    return rank

Upvotes: 2

John Rouhana
John Rouhana

Reputation: 548

I can tell you that if you hit your 'break' statement, you're not going to be returning anything, because you've got your indentation wrong. You need to take one indentation out of your return.

I can also tell you a little bit about global and local variables, in lay-terms: Any variable you define inside your function, such as 'rank', will not persist outside the function. If you run the above and then try to call 'rank' outside of your function, you'll be calling a blank variable (unless you specifically named another variable 'rank'). This enables you to use rank inside the function without fearing conflict with variables outside the function. In general, you want to avoid making any truly 'global' variables, but a variable named outside of a function is more 'global' than one named inside one.

As for what pyCharm is flagging, I'm not sure, because I don't use the program. Are you calling your function anywhere? It could be letting you know that you're defining your function, and then not calling it.

Upvotes: 0

Related Questions