简体   繁体   English

为什么我在 function 中使用局部变量时出现错误,但它是全局变量

[英]Why am I getting an error that I'm using a local variable inside a function when it is a global variable

I have a code right now that I'm working on and for some reason whenever I run it my error is "local variable 'buzzcoins' referenced before argument".我现在有一个正在处理的代码,出于某种原因,每当我运行它时,我的错误是“在参数之前引用了局部变量'buzzcoins'”。

As you can see buzzcoins is defined out of a function therefore it should be global?如您所见,buzzcoins 是从 function 定义的,因此它应该是全局的? If anyone could help me with this that would be a huge help!如果有人可以帮助我,那将是一个巨大的帮助!

MY CODE:我的代码:

import os
import random
import time
from keepalive import keep_alive
from discord.ext import commands

#COINS
buzzcoins = 1000

#COMMAND PREFIX
client = commands.Bot(command_prefix = "!")

#ON_READY MESSAGE
@client.event
async def on_ready():
  await client.change_presence(activity=discord.Game("#commands"))
  print("Bot is now running...")

#DICE COMMAND
@client.command()
async def dice(message, amount:int):
  #DICE COMMAND - CHECKS
  if amount > buzzcoins:
    await message.channel.send("You bet more Buzz Coins then this server has!")
  elif amount > 500:
    await message.channel.send("You cannot bet more than 500 Buzz Coins at a time!")
  elif amount == 0:
    await message.channel.send("You cannot bet 0 Buzz Coins!")
  elif amount < 0:
    await message.channel.send("You cannot bet less than 0 Buzz Coins!")
  else:
    await message.channel.send(f"Betting {amount} Buzz Coins!")
    time.sleep(1)
    await message.channel.send(":game_die: Rolling the dice... :game_die:")

    playerdicenumber1 = random.randint(1, 6)
    playerdicenumber2 = random.randint(1, 6)
    time.sleep(3)
    await message.channel.send(f"You roll a {playerdicenumber1} and a {playerdicenumber2}!")

    botdicenumber1 = random.randint(1, 6)
    botdicenumber2 = random.randint(1, 6)
    time.sleep(3)
    await message.channel.send(f"Your opponent rolls a {botdicenumber1} and a {botdicenumber2}!")
    time.sleep(1)
  
  #DICE COMMAND - WIN
  if playerdicenumber1 + playerdicenumber2 > botdicenumber1 + botdicenumber2:
    await message.channel.send(f"You won {amount*2} Buzz Coins! :smile:")
    buzzcoins = buzzcoins + amount
    time.sleep(1)
    await message.channel.send(f"This server now has {buzzcoins} Buzz Coins!")

  #DICE COMMAND - LOSE
  elif playerdicenumber1 + playerdicenumber2 < botdicenumber1 + botdicenumber2:
    await message.channel.send(f"You lost {amount} Buzz Coins! :cry:")
    buzzcoins = buzzcoins - amount
    time.sleep(1)
    await message.channel.send(f"This server now has {buzzcoins} Buzz Coins!")

  #DICE COMMAND - TIE
  elif playerdicenumber1 + playerdicenumber2 == botdicenumber1 + botdicenumber2:
    await message.channel.send(f"You tied! Your bet has been returned! :ok_hand:")
    time.sleep(1)
    await message.channel.send(f"This server now has {buzzcoins} Buzz Coins!")

#KEEP_ALIVE
keep_alive()
my_secret = os.environ['token']
client.run(my_secret)

You need to specify what it is global:您需要指定它是全局的:

  ...
  async def dice(message, amount:int):
    #DICE COMMAND - CHECKS
    global buzzcoins # add this line
    if amount > buzzcoins:
  ...

You need to add global buzzcoins inside the function.您需要在 function 中添加global buzzcoins Buzzcoins。 You are getting the error because right now, the interpreter is trying to find a definition of buzzcoins inside the function and not in the global scope.您收到错误是因为现在,解释器正试图在 function 而不是全局 scope 中找到 Buzzcoins 的定义。

Instead of using global, make your variable a class variable of the bot object而不是使用全局,使您的变量成为机器人 object 的 class 变量

ie: instead of this,即:而不是这个,

#COINS
buzzcoins = 1000

#COMMAND PREFIX
client = commands.Bot(command_prefix = "!")

do this,做这个,

#COMMAND PREFIX
client = commands.Bot(command_prefix = "!")\

#COINS
client.buzzcoins = 1000

then you can access it without using the global keyword in your function:那么您可以在不使用 function 中的global关键字的情况下访问它:

async def dice(ctx, amount:int):
    #DICE COMMAND - CHECKS
    if amount > client.buzzcoins:
       client.buzzcoins += 100 # example

also message in your function is not a discord.Message object, but a commands.Context object.在您的 function 中还有message不是discord.Message .消息 object,而是commands.Context 。上下文 ZA8CFDE63311C6666 You can use shortcut for send , ie, await ctx.send("blah blah") istead await ctx.channel.send("blah blah")您可以使用快捷方式send ,即await ctx.send("blah blah")而不是await ctx.channel.send("blah blah")

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么在尝试用python编程堆栈时将局部变量声明为全局变量时出现局部变量错误? - why am I getting an error of local variable when I have declared it as a global variable, whilst trying to program a stack in python? 为什么会出现错误“赋值之前引用本地变量”或“未定义全局变量”的错误? - Why am I getting either error “local variable referenced before assignment” or “global variable not defined”? 为什么我在全局变量编辑时遇到Type错误? - Why am i getting a Type error with global variable editing? 为什么在定义变量时出现名称错误? - Why am I getting a name error when I defined the variable? 为什么当我在其中使用局部变量时,eval function 无法识别三角函数? - why eval function is not recognizing trigonometric when I am using local variable in it? 为什么我得到变量未定义错误? - Why am I getting the variable not defined error? 为什么我没有得到这个变量的索引错误? - Why i am getting not index error with this variable? 为什么我在 python 的全局声明之前得到“变量分配” - why am i getting "variable assigned befor global declaration on python 为什么我在上面定义了变量时会收到“未定义的变量”错误 - Why am I getting a 'undefined variable' error when I have defined the variable above 我正在尝试为我正在开发的应用程序制作 GUI,但是当我尝试打印全局变量时出现错误。 为什么? - I am trying to make a GUI for a app I am working on but when I try print a global variable I get an error. Why?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM