python - discord.py 不执行数学/python 代码
问题描述
我有这个兼作 python 命令的数学代码,我希望它解决数学问题。当我说p!math 3+13
时,它应该发送 16,但它会将问题 (3+13) 发送回用户。有人可以帮忙吗?
@client.command()
async def math(ctx, *, p):
try:
exec(p)
await ctx.send(p)
except Exception as e:
await ctx.send(f"```python\nError: {e}```")
解决方案
您使用了不正确的功能。不要使用 exec,而是使用 eval。此外,您应该将返回值设置为参数并将其返回。
例如:
@client.command()
async def math(ctx, *, p):
try:
answer = eval(p)
await ctx.send(answer)
except Exception as e:
await ctx.send(f"```python\nError: {e}```")
推荐阅读
- css - 是否有防止视频标题被截断的解决方案?
- node.js - NestJS 导出。连接,postgres 多租户
- javascript - 角型错误
- json - 用 jq 合并两个 JSON 对象
- visual-studio-2017 - 用户在 vs setup 项目中卸载应用程序时如何删除应用程序生成的配置文件
- django-rest-framework - DRF - 具有多个序列化器或重复序列化器的 API
- javascript - 基于出生日期的数据表搜索年龄
- javascript - 如何提高键盘导航性能
- machine-learning - Adaptive_avg_pool2d 的计算 FLOPs
- javascript - 如何在此 HTML 页面中存储数据并使用 JavaScript 在另一个页面中检索它