【问题标题】:Error while using functions in python (Discord.py)在 python (Discord.py) 中使用函数时出错
【发布时间】:2021-01-15 04:46:21
【问题描述】:

我不能从另一个文件调用函数。 我的代码:

def standard(title, desc, color):
    return color = embeds.colors. + color
    return embed=discord.Embed(title=title, description=desc, color=color)
    return embed.set_footer(text=embeds.settings.footer)

错误代码:

Traceback (most recent call last):
  File "C:\Users\def75\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\ext\commands\core.py", line 85, in wrapped
    ret = await coro(*args, **kwargs)
  File "C:\Users\def75\Desktop\discordbot\new\cogs\fun.py", line 22, in _8ball
    await ctx.send(embed=embed)
UnboundLocalError: local variable 'embed' referenced before assignment

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "C:\Users\def75\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\ext\commands\bot.py", line 903, in invoke
    await ctx.command.invoke(ctx)
  File "C:\Users\def75\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\ext\commands\core.py", line 855, in invoke
    await injected(*ctx.args, **ctx.kwargs)
  File "C:\Users\def75\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\ext\commands\core.py", line 94, in wrapped
    raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: UnboundLocalError: local variable 'embed' referenced before assignment

C:\Users\def75\Desktop\discordbot\new>python main.py
Traceback (most recent call last):
  File "main.py", line 4, in <module>
    from config import *
  File "C:\Users\def75\Desktop\discordbot\new\config.py", line 24
    return color = embeds.colors. + color
                 ^
SyntaxError: invalid syntax

整个函数在类中:embeds.definitions。 (是的,这是两个类)

这发生在任何函数上

【问题讨论】:

  • 嵌入需要在范围内。没有更多上下文,一些可能性:它必须被导入,它必须从另一个模块或实例中引用

标签: python function discord discord.py


【解决方案1】:

这确实是一个语法错误。你是returning 一个任务

return x = y

x = y 实际上不包含任何值,而是将值 y 放在变量 x 内,所以你不能 return 它(它是 does 的东西,但没有give你什么)。

我不明白你为什么要退回所有这些东西?您可能想了解return 的工作原理。

【讨论】:

    【解决方案2】:

    当你在 Python 中使用return 函数时,你不能在一个函数中多次使用它。 return 返回一个值并退出函数。

    您也不能在return 中使用=。您可以像这样更改代码:

    def standard(title, desc, color):
        embed = discord.Embed(title=title, description=desc, color=color)
        embed.set_footer(text=embeds.settings.footer)
        return embed
    

    【讨论】:

    • 我发送的代码中不能有语法错误。你在哪一行得到错误?
    猜你喜欢
    • 2015-02-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多