Python 在异步函数中赋值之前引用的局部变量,已赋值同名的全局变量
在过去的几天里,我一直在开发一个Discord机器人。在我遇到这个问题之前,一切都很好:Python 在异步函数中赋值之前引用的局部变量,已赋值同名的全局变量,python,asynchronous,global-variables,discord,Python,Asynchronous,Global Variables,Discord,在过去的几天里,我一直在开发一个Discord机器人。在我遇到这个问题之前,一切都很好: said_yes=False 说“不”=错 @discordclient.event _ready()上的异步定义: . . . @discordclient.event 异步def on_消息(消息): 如果message.content=='.blackjack': . . . 如果message.content.lower() 说“是”=真; . . . elif message.content
said_yes=False
说“不”=错
@discordclient.event
_ready()上的异步定义:
. . .
@discordclient.event
异步def on_消息(消息):
如果message.content=='.blackjack':
. . .
如果message.content.lower()
说“是”=真;
. . .
elif message.content.lower()=='no'并说_yes==False:
说不等于真;
. . .
elif message.content.lower()!='。blackjack'和said_yes==假和said_no==假:
. . .
错误消息是:
File "filename.py", line 95, in on_message
elif message.content.lower() == 'no' and said_yes == False:
UnboundLocalError: local variable 'said_yes' referenced before assignment
此错误发生在第二个if
语句以及第一个和第二个elif
语句上。唯一的区别是变量(表示“是”
或表示“否”
)
如何修复此错误?我希望说的是
和说的不是
变量在全局范围内