繁体   English   中英

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

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

我现在有一个正在处理的代码,出于某种原因,每当我运行它时,我的错误是“在参数之前引用了局部变量'buzzcoins'”。

如您所见,buzzcoins 是从 function 定义的,因此它应该是全局的? 如果有人可以帮助我,那将是一个巨大的帮助!

我的代码:

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)

您需要指定它是全局的:

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

您需要在 function 中添加global buzzcoins Buzzcoins。 您收到错误是因为现在,解释器正试图在 function 而不是全局 scope 中找到 Buzzcoins 的定义。

而不是使用全局,使您的变量成为机器人 object 的 class 变量

即:而不是这个,

#COINS
buzzcoins = 1000

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

做这个,

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

#COINS
client.buzzcoins = 1000

那么您可以在不使用 function 中的global关键字的情况下访问它:

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

在您的 function 中还有message不是discord.Message .消息 object,而是commands.Context 。上下文 ZA8CFDE63311C6666 您可以使用快捷方式send ,即await ctx.send("blah blah")而不是await ctx.channel.send("blah blah")

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM