Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/354.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用Discord.py rewrite-局部变量';柜台';分配错误前引用_Python_Python 3.6_Discord.py Rewrite - Fatal编程技术网

Python 使用Discord.py rewrite-局部变量';柜台';分配错误前引用

Python 使用Discord.py rewrite-局部变量';柜台';分配错误前引用,python,python-3.6,discord.py-rewrite,Python,Python 3.6,Discord.py Rewrite,我试图在discord机器人中创建一个计数器,从用户那里获取一个数字并将其添加到当前总数中。但是,我得到一个“赋值前引用的局部变量‘计数器’错误”或“未定义名称计数器”错误 在试图解决问题时,我尝试了两种变体 差异1: global counter @bot.command() async def bid(ctx,number): counter += number print(str(counter)) 变式2: @bot.command() async def bid(ct

我试图在discord机器人中创建一个计数器,从用户那里获取一个数字并将其添加到当前总数中。但是,我得到一个“赋值前引用的局部变量‘计数器’错误”或“未定义名称计数器”错误

在试图解决问题时,我尝试了两种变体

差异1:

global counter
@bot.command()
async def bid(ctx,number):
    counter += number
    print(str(counter))
变式2:

@bot.command()
async def bid(ctx,number):
    global counter
    counter += 1
    print(str(counter))  
变体1取自StackOverflow的类似问题帖子。这是返回“赋值前引用的局部变量‘计数器’错误”的代码

变体2也取自一个类似的问题(同一个问题实际上只是一个不同的回答)。这是返回“未定义名称计数器”错误的代码

预期结果:

我希望创建一个命令,允许用户增加计数。因此,假设计数当前为0。我想能够说!出价40,数到40。如果其他用户说!出价20,我要数到60


提供的代码中到底有什么问题?为什么会这样

在变体2中,您必须定义计数器:

counter = 0

@bot.command()
async def bid(ctx,number):
    global counter
    counter += 1
    print(str(counter))