0

I am trying to create a variable that every time a command/function is called upon, it will add to the variable.

import discord
import asyncio
from discord.ext import commands
botToken = 'TOKEN'
bot = commands.Bot(command_prefix = '#')
@bot.event
async def on_ready():
    print('Bot is online and ready.')
    varible1 = 1
async def changeVariable(ctx, *, orderItem):
    varible1 = variable1 + 1
    print(variable1)

It tells me that the variable is not assigned/referenced before assignment. Is there a way to make like a global function? I can try to give more details if needed.

EchoTheAlpha
  • 422
  • 2
  • 8
  • 27

1 Answers1

0

You could make a global variable at the top of the file below the imports. Then just increment the variable any time you enter a function, as shown below.

import discord
import asyncio
from discord.ext import commands

function_call_count = 0 

botToken = 'TOKEN'
bot = commands.Bot(command_prefix = '#')
@bot.event

async def on_ready():
    function_call_count += 1
    print('Bot is online and ready.')
    varible1 = 1

async def changeVariable(ctx, *, orderItem):
    function_call_count += 1
    varible1 = variable1 + 1
    print(variable1)
  • This will still fail, because Python treats assignment as creating local variables unless explicitly instructed otherwise. You would need to add `global function_call_count` to let the interpreter know that you want to assign to a variable in another context. – Patrick Haugh Jun 21 '20 at 02:25