Home > OS >  local variable 'count' referenced before assignment
local variable 'count' referenced before assignment

Time:08-21

Want it to count everytime someone says hi or hey, but I cant make the variable to 0

 @client.event
    async def on_message(ctx):
        ctx.content=ctx.content.lower()
        if 'hi' in ctx.content:
            count = count   1
        if 'hey' in ctx.content:
            count = count   1
        if ctx.content == "!count":
            print("yes")
            await ctx.channel.send(count)

CodePudding user response:

Declare count at the start before you start counting

CodePudding user response:

It looks like count is defined after the calls to it. Try moving that line above the function definition:

count = 0
@client.event
    async def on_message(ctx):
    # -- snip --

... or

@client.event
async def on_message(ctx):
    count = 0
    # -- snip --

(FYI, this code will not persist the count, e.g. it will be reset every time the script reboots.)

  • Related