[英]UnboundLocalError: Local variable 'user' referenced before assignment
This is part of a code that runs a moderator bot in a chatroom i use. 这是在我使用的聊天室中运行主持人机器人的代码的一部分。 This section of the code is to approve someone's request to cam up but whenever I use the command I get this unbound local error... I have gone through this so many times and I can't figure out why I'm getting it.
代码的这一部分是批准某人的请求,但是每当我使用该命令时,我都会收到此未绑定的本地错误...我已经经历了很多次,而且我不知道为什么要得到它。
def approveCam(room, identifier):
if not room.bpass:
return
if type(identifier) in [str, unicode, int]:
user = room._getUser(identifier)
if not user:
return "User " + str(identifier) + " was not found..."
if user.broadcasting:
return
room._sendCommand("privmsg", [room._encodeMessage("/allowbroadcast " + room.bpass),
"#0,en" + "n" + str(user.id) + "-" + user.nick])
The problem seems to be at "if user.broadcasting:" 问题似乎出在“ if user.broadcasting:”
the code worked on a previous version of the bot like this 该代码可在这样的旧版漫游器上正常工作
def approveCam(room, user):
if type(user) is str or type(user) is unicode:
nick = user
user = room._getUser(user)
if not user:
return "User "+nick+" was not found..."
if not room.bpass:
return
room._sendCommand("privmsg", [room._encodeMessage("/allowbroadcast "+room.bpass),
"#0,en"+"n"+ user.id+"-"+user.nick])
and here is the response i get in command prompt when i try to run the command. 这是我尝试运行命令时在命令提示符下得到的响应。
Traceback (most recent call last):
File "C:\Users\Ejah\Downloads\Desktop\Tunebot-Master\tinychat.py", line 1262
in onMessage
SETTINGS['onMessageExtend'](self, user, msg)
File "tunebot.py", line 1316, in onMessageExtended
handleUserCommand(room, user, msg)
File "tunebot.py", line 1722, in handleUserCommand
res = botterCommands(room, userCmd, userArgsStr, userArgs, target,
File "tunebot.py", line 2786, in botterCommands
res = approveCam(room, user)
File "tunebot.py", line 4043, in approveCam
if user.broadcasting:
UnboundLocalError: local variable 'user' referenced before assignment"
Update your code to raise an error when identifier
is of an invalid type and all will become clear: 当
identifier
为无效类型并且所有内容都将变得清楚时,请更新代码以引发错误:
def approveCam(room, identifier):
if not room.bpass:
return
if type(identifier) in [str, unicode, int]:
user = room._getUser(identifier)
if not user:
return "User " + str(identifier) + " was not found..."
else:
raise ValueError('Invalid type for identifier')
if user.broadcasting:
return
room._sendCommand("privmsg", [room._encodeMessage("/allowbroadcast " + room.bpass),
"#0,en" + "n" + str(user.id) + "-" + user.nick])
user.broadcasting - This is not correct
At this point user does not exist and hence the interpreter won't allow that. 此时,用户不存在,因此解释器不允许这样做。 You must initialise local variables before using them.
您必须先初始化局部变量,然后才能使用它们。
Make user a global variable with some value to it. 使用户成为具有某些值的全局变量。
Probably if type(identifier) in [str, unicode, int]:
is False
, so the body of the if
is not executed and user
is never inizialized. if type(identifier) in [str, unicode, int]:
可能为False
,那么if
的主体将不会执行,并且user
也不会被初始化。
Initialize user
before the second if
if possible, or rethink your code. 初始化
user
之前第二if
可能的话,或重新考虑你的代码。
PS Don't use getter and setter! PS不要使用getter和setter! Python is not Java, if you really need to use them, use a property instead.
Python不是Java,如果确实需要使用它们,请改用属性。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.