Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Updating variable every 24 hours

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading