【问题标题】:Why am I getting an error that I'm using a local variable inside a function when it is a global variable为什么我在函数中使用局部变量时出现错误,而它是全局变量
【发布时间】:2022-01-26 04:35:36
【问题描述】:

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

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

我的密码:

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)

【问题讨论】:

    标签: python discord discord.py


    【解决方案1】:

    你需要指定它是全局的:

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

    【讨论】:

    • 谢谢弗拉迪斯拉夫!我刚刚做了buzzcoins = globals()['buzzcoins'],它成功了!
    • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center
    【解决方案2】:

    您需要在函数内部添加global buzzcoins。 您收到错误是因为现在,解释器正试图在函数内而不是在全局范围内找到 Buzzcoins 的定义。

    【讨论】:

      【解决方案3】:

      不要使用全局变量,而是让你的变量成为机器人对象的类变量

      即:而不是这个,

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

      这样做,

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

      那么您可以在函数中不使用global 关键字来访问它:

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

      你的函数中的message 也不是discord.Message 对象,而是commands.Context 对象。您可以使用send 的快捷方式,即await ctx.send("blah blah") 而不是await ctx.channel.send("blah blah")

      【讨论】:

        猜你喜欢
        • 2021-06-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多