微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决在python中使用函数时出错Discord.py

我无法从另一个文件调用函数。 我的代码

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中使用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
,

这确实是语法错误。您是returning的任务

return x = y

x = y实际上不保存任何值,而是将值y放在变量x内,因此您不能return对其({ {1}}的东西,但does却什么都没有。

我不明白为什么您要退回所有这些东西?您可能想研究give的工作方式。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。