Python 我得到UnboundLocalError,因为我在函数和主代码中有相同的变量

Python 我得到UnboundLocalError,因为我在函数和主代码中有相同的变量,python,function,python-3.x,syntax-error,Python,Function,Python 3.x,Syntax Error,我有一个噩梦,我用Python创建了一个代码来模拟一个虚拟队列 代码过于简单,但应该可以工作,除了我不断得到的反复出现的'unbundLocalError'之外 我认为问题在于将'aList'作为函数的参数,并在主代码以及def函数中包含变量 代码没有那么长,可以在上找到 有人能帮忙吗?我尝试添加了“全局列表”,但没有解决问题 非常感谢您的帮助 代码快照如下: def aRemove(aList): newlist = [] for i in range(1,len(aList)

我有一个噩梦,我用Python创建了一个代码来模拟一个虚拟队列

代码过于简单,但应该可以工作,除了我不断得到的反复出现的
'unbundLocalError'
之外

我认为问题在于将
'aList'
作为函数的参数,并在主代码以及
def
函数中包含变量

代码没有那么长,可以在上找到

有人能帮忙吗?我尝试添加了
“全局列表”
,但没有解决问题

非常感谢您的帮助

代码快照如下:

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)

问题是aList还没有指定的值。我个人只想声明

aList = []
在您的代码中,只要在用户请求声明之前,它就可以工作。尽管根据Satish的解决方案,您也可以将其传递给函数

另一个问题是删除函数

for i in range(1,len(aList)):
应改为

for i in range(1,len(aList)-1):
否则,您将尝试附加一个不存在的元素。 尽管由于它是python,您可以这样做:

aList = aList[1:]

代码中的错误不是因为您在问题中添加了代码段,而是查看了链接中提供的代码

您正在使用
aList
内部函数
user\u request()
,但它没有在任何地方声明。你能做的事情如下:

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()
user_request(aList) 
并按如下方式替换调用函数:

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()
user_request(aList) 
这将解决你的错误


注意:请始终读取发生错误的行号,因为这将帮助您更快、更有效地调试错误。

您看到的错误不是由您发布的代码片段引起的,而是由用户请求函数(来自您的pastebin代码)引起的。在其中,您试图引用全局
aList
变量,但也重新分配了一个名为
aList
的局部变量

为了解释正在发生的事情,python假设
aRemove(aList)
引用的是局部aList变量,而该变量目前还不存在

您可以通过两种方式解决此问题:

在函数中添加
global-aList
,告诉python本地
aList
与全局
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()
或者,重命名本地
aList
变量,使其不再与全局变量冲突:

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()

@阴影游侠-谢谢你的帮助编辑。不客气。代码块只需要额外的四个空格缩进。在任何情况下,您提供的代码段都可以很好地工作(除了
user\u request()
调用,您还没有定义它,所以我不知道它是否工作)。它有误导性(分配给
aList
不会改变调用者,它会返回新的
列表
,并让调用者重新绑定以修复它),但它可以正常工作。要么你没有你所说的问题,要么你没有提供触发它的代码。提供错误的回溯,以及与该回溯相关的代码。@ShadowRanger-我只在我的消息中放了一段代码(因为它有64行长)(但是消息中有一个指向完整代码的链接。我得到的错误是“用户请求列表中的第55行=(aRemove(aList))UnboundLocalError:在赋值之前引用了局部变量“aList”。我读过关于非局部变量和全局变量的书,但我无法解决问题,而且愚蠢地说我已经没有时间了。有什么建议吗?你在哪里添加了
全局列表
?当我将其添加到
用户请求
时,你的问题就不再出现了。编辑后代码的格式正确吗?因为如果是这样,就不可能出现错误;
aList
在声明错误发生前的一行,在同一范围内有一个绑定。如果代码错误/不完整,请编辑您的问题(请记住添加额外的四个前导空格,使其格式为代码块)。在python中使用全局变量是不好的做法。请阅读以下内容:python没有变量减速。尚未赋值…这是一个语义问题,而不是技术问题。尽管进行了编辑。鉴于我的句子后面有一个直接的示例,说明了我的意思,它应该非常清楚。我不是指java中的声明,如中的
t变量
但事实上,他并没有声明从现在起将有一个列表变量。