0

I am having a nightmare, I have created a code in Python to simulate a virtual queue.

The code is simplistic but should work, except for the reoccurring 'UnboundLocalError' I keep getting.

I think the issue is having 'aList' as a parameter for functions and having the variable in the main code as well as within def functions.

The code isn't that long and can be found on this link.

Please can somebody help? I've tried adding 'global aList' but it didn't solve it.

Thanks very much for any assistance.

Snapshot of code below :

def aRemove(aList):
    newlist = []
    for i in range(1,len(aList)):
        newlist.append(aList[i])
    aList = newlist
    return (aList)

user_request()
aList = ["mark","jon","peter"]
aList = (aRemove(aList))
print(aList)
Satish Prakash Garg
  • 2,213
  • 2
  • 16
  • 25
  • @ShadowRanger - Thanks for your help editing. – kingpete84 Mar 29 '17 at 01:08
  • 1
    You're welcome. All you need for code blocks is an extra four space indent. In any event, that snippet you provided works perfectly fine as written (aside from the `user_request()` call, which you haven't defined, so I have no idea if it works). It's misleading (assigned to `aList` doesn't change the caller, it's returning the new `list` and having the caller rebind that fixes it), but it works fine as is. Either you don't have the problem you say you have, or you haven't provided the code that triggers it. Provide the traceback from the error, and the code associated with that traceback. – ShadowRanger Mar 29 '17 at 01:10
  • @ShadowRanger - I only put a snippet of the code (as its 64 lines long) on my message(there is a link to full code in the message though. The error I get is "line 55, in user_request aList = (aRemove(aList)) UnboundLocalError: local variable 'aList' referenced before assignment". I've read about nonlocal variables and global variables but I can't figure out the problem and stupidly I've ran out of time. Any suggestions? – kingpete84 Mar 29 '17 at 01:22
  • Where did you add `global aList`? When I add it to `user_request`, your issue no longer occurs. – ForeverWintr Mar 29 '17 at 01:28
  • 1
    Is the code formatted correctly after my edit? Because if so, that error isn't possible; `aList` is given a binding in the same scope just one line before you claim the error occurs. If the code is wrong/incomplete, edit your question (just remember to add the extra four leading spaces so it formats as a code block). – ShadowRanger Mar 29 '17 at 01:29
  • @ForeverWintr I added 'global aList' to the next line after 'def userrequest() and I still got errors? – kingpete84 Mar 29 '17 at 01:30
  • Why don't you update your question to include the code that actually produces the error, as well as what you've tried. Adding the global keyword should fix it. – ForeverWintr Mar 29 '17 at 01:32

3 Answers3

0

Problem is that aList does not have an assigned value yet. I personally would just declare

aList = []

in the very beggining of your code, but it will work as long as it is before the user_request declaration. Although you could pass it along to your function as well, as per Satish's solution.

Another issue you have is in the remove function.

for i in range(1,len(aList)):

should be changed to

for i in range(1,len(aList)-1):

Otherwise you're trying to append an element that does not exist. Although since it's python you could just do this:

aList = aList[1:]
  • 1
    Python doesn't have variable deceleration. – juanpa.arrivillaga Mar 29 '17 at 01:48
  • is not *assigned* yet... That's a semantics issue, not technical issue. Edited though. Given that my sentence was followed by a direct example of what i mean it should've been quite clear. I did not mean a declaration as in java such as `int variable` but the fact that he has not declared that there's an aList variable that will be a list from now on. – Edwin Kravčenka Mar 29 '17 at 01:50
0

The error in the code is not because of the code snippet you have added in the question but looking at your code provided in the link.

You are using aList inside function user_request() but it is not declared anywhere. What you can do something like this :

def user_request(aList):                 
    order = input("What would you like to do P | A | N | L | M | Q \n")
    while True:
        if order == 'P':
            aPrint()
        elif order == 'A':
            useradd = input("what would you like to add to the queue?")
            aAdd(useradd)
        elif order == 'N':
            aList = (aRemove(aList))   # aList used without passed
        elif order == 'L':
            aList = (aLeave(aList,input("What would you like to remove from the queue?")))
        elif order == 'M':
            aLocation()
        elif order == 'Q':
            aQuit()

and replace the calling function like this :

user_request(aList) 

This shall resolve your error.

Note : Always read the line number in which error occur as that will help you in debugging the error faster and more efficiently.

Satish Prakash Garg
  • 2,213
  • 2
  • 16
  • 25
0

The error you're seeing isn't caused by the code snippet you've posted, but rather the user_request function (from your pastebin code). In it, you're trying to reference the global aList variable, but you're also reassigning a local variable called aList.

To attempt to paraphrase what's happening, python assumes aRemove(aList) is referring to the local aList variable, which doesn't exist yet.

You can fix this in two ways:

add global aList within your function to tell python that your local aList is the same as the global aList:

def user_request():                 
    global aList
    order = input("What would you like to do P | A | N | L | M | Q \n")
    while True:
        if order == 'P':
            aPrint()
        elif order == 'A':
            useradd = input("what would you like to add to the queue?")
            aAdd(useradd)
        elif order == 'N':
            aList = (aRemove(aList))
        elif order == 'L':
            aList = (aLeave(aList,input("What would you like to remove from the queue?")))
        elif order == 'M':
            aLocation()
        elif order == 'Q':
            aQuit()

Or, rename your local aList variable so that it no longer collides with the global variable:

def user_request():               
    order = input("What would you like to do P | A | N | L | M | Q \n")
    while True:
        if order == 'P':
            aPrint()
        elif order == 'A':
            useradd = input("what would you like to add to the queue?")
            aAdd(useradd)
        elif order == 'N':
            bList = (aRemove(aList))
        elif order == 'L':
            bList = (aLeave(aList,input("What would you like to remove from the queue?")))
        elif order == 'M':
            aLocation()
        elif order == 'Q':
            aQuit()
ForeverWintr
  • 5,492
  • 2
  • 36
  • 65