Advertisements
I’m trying to add 1 to a variable every (2 seconds for this example). Problem is, the variable only ever updates to 1. Am I overlooking something?
Code:
import discord
from discord.ext import commands, tasks
countvar = 0 # Var defined.
@bot.event
async def on_ready():
counter.start(countvar) # loop started
@tasks.loop(seconds=2)
async def counter(countvar):
countvar += 1 # every 2 seconds add 1 to var
print(f'countvar + 1 ({countvar})')
Output:
countvar + 1 (1)
countvar + 1 (1)
countvar + 1 (1)
Expected Result:
countvar + 1 (1)
countvar + 1 (2)
countvar + 1 (3)
>Solution :
Use global keyword to modify the global copy of your variable.
async def counter(countvar):
global countvar
countvar += 1 # every 2 seconds add 1 to var