在python中使用函数时出错(Discord.py)

在python中使用函数时出错(Discord.py),python,function,discord,discord.py,Python,Function,Discord,Discord.py,我无法从其他文件调用函数。 我的代码: 错误代码: 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

我无法从其他文件调用函数。 我的代码:

错误代码:

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
回溯(最近一次呼叫最后一次):
文件“C:\Users\def75\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\ext\commands\core.py”,第85行,包装为
ret=等待coro(*args,**kwargs)
文件“C:\Users\def75\Desktop\discordbot\new\cogs\fun.py”,第22行,在8ball中
等待ctx.send(嵌入=嵌入)
UnboundLocalError:赋值前引用了局部变量“嵌入”
上述异常是以下异常的直接原因:
回溯(最近一次呼叫最后一次):
文件“C:\Users\def75\AppData\Local\Programs\Python\Python38-32\lib\site packages\discord\ext\commands\bot.py”,第903行,在invoke中
等待ctx.command.invoke(ctx)
文件“C:\Users\def75\AppData\Local\Programs\Python\Python38-32\lib\site packages\discord\ext\commands\core.py”,调用中第855行
等待注入(*ctx.args,**ctx.kwargs)
文件“C:\Users\def75\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\ext\commands\core.py”,第94行,包装为
从exc引发CommandInvokeError(exc)
discord.ext.commands.errors.CommandInvokeError:命令引发异常:UnboundLocalError:赋值前引用的局部变量“嵌入”
C:\Users\def75\Desktop\discordbot\new>python main.py
回溯(最近一次呼叫最后一次):
文件“main.py”,第4行,在
从配置导入*
文件“C:\Users\def75\Desktop\discordbot\new\config.py”,第24行
返回颜色=embeds.colors.+颜色
^
SyntaxError:无效语法
整个函数在类中:embeds.definitions。(是的,那是两门课)


任何函数都会发生这种情况,当您在Python中使用
return
函数时,在一个函数中不能多次使用它<代码>返回返回一个值并退出函数

您也不能在
返回中使用
=
。您可以按如下方式更改代码:

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

这确实是一个语法错误。您正在返回作业

return x = y
x=y
实际上并不包含任何值,而是将值
y
放入变量
x
中,因此您不能
返回它(它
做了一些事情,但
没有给你任何东西)


我不明白你为什么要把这些东西都还给我?您可能想了解
return
是如何工作的。

embeds需要在范围内。如果没有更多的上下文,就有一些可能:它必须被导入,必须从另一个模块或实例引用。我发送的代码中不能有语法错误。您得到的错误是哪一行?
return x = y