英文:
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}")
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论