這是在我使用的聊天室中運行主持人bot的代碼的一部分。代碼的這一部分是批准某人的請求凸起,但每當我使用命令我得到這個未綁定的本地錯誤... 我已經經歷了這麼多次,我不明白爲什麼我得到它。UnboundLocalError:分配之前引用的本地變量'user'
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])
這個問題似乎是在「如果user.broadcasting:」
的代碼工作在以前版本的機器人像這樣
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])
,這裏是我得到的迴應在命令提示符下,當我嘗試運行該命令時。
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"
如果你的第二個'if'語句是False(你在比較'identifier'的地方),你不會爲'user'分配任何東西。所以它不存在 –
請正確縮進您的代碼 – donkopotamus