创建一个Discord机器人 – 返回值的问题

huangapple go评论99阅读模式
英文:

creating a discord bot - problems with returning values

问题

我是新手学习Python,尝试创建一个Discord机器人,可以将用户输入保存到SQL数据库中,然后可以在以后进行检索。

已经成功保存了数据,一切都正常工作,问题出在用户尝试检索他们提交的数据时,我无法让它实际发送变量中的数据,而是只发送变量名。

我似乎无法弄清楚为什么会这样,尽管我进行了谷歌搜索。有人可以帮助我吗?涉及的代码如下:

async def picks(ctx):
    cp1 = cursor.execute("SELECT currentpick1 FROM nascarpool WHERE id = '{ctx.author.id}'")
    cp2 = cursor.execute("SELECT currentlpick2 FROM nascarpool WHERE id = '{ctx.author.id}'")
    cp3 = cursor.execute("SELECT currentlpick3 FROM nascarpool WHERE id = '{ctx.author.id}'")
    cursor.execute(cp1, cp2, cp3)
    await ctx.send(f"You have registered the following picks: {cp1}, {cp2}, and {cp3}")
    cursor.close()
    mydb.close()

输出正好如输入,"You have registered the following picks: {cp1}, {cp2}, and {cp3}"

提前感谢!

英文:

I'm new to python and trying to create a discord bot that saves user input into an SQL database, which then can then recall later on.

I've got it saving and all is working there, the problem comes when the user tries to recall the data they have submitted, in that I can't get it to actually send the data in the variable, instead it just sends the variable name.

I cant seem to figure out why this is, regardless of my googling. Could someone help me out here? Code in question:

async def picks(ctx):
    cp1 = cursor.execute("SELECT currentpick1 FROM nascarpool WHERE id = '{ctx.author.id}'")
    cp2 = cursor.execute("SELECT currentlpick2 FROM nascarpool WHERE id = '{ctx.author.id}'")
    cp3 = cursor.execute("SELECT currentlpick3 FROM nascarpool WHERE id = '{ctx.author.id}'")
    cursor.execute(cp1, cp2, cp3)
    await ctx.send("You have registered the following picks: {cp1} {cp2}, and {cp3}")
    cursor.close
    mydb.close

Output is exactly as its typed, "You have registered the following picks: {cp1}, {cp2} and {cp3}"

Thanks in advance!

答案1

得分: 0

在引号前加上 f 以创建格式化字符串文字
所以你会有
```python
await ctx.send(f"您已注册以下选择:{cp1} {cp2}{cp3}")
英文:

Precede the quote with f for formatted string literals.
So you will have:

await ctx.send(f"You have registered the following picks: {cp1} {cp2}, and {cp3}")

huangapple
  • 本文由 发表于 2023年8月10日 09:38:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/76872142.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定