我是python新手,正在使用discord.py开发discord机器人。我试图使它这样当你键入%quote时,它会从文本文件中随机得到一行。但出于某种原因,它跳过了第一行或第二行。也许我把逻辑搞错了?这是我的密码
@commands.command()
async def quote(self, ctx, Qnum='janfol91213sdf2uieh1あ2Ⅳ3sんksnaaksd'):
i = 0
if Qnum == 'janfol91213sdf2uieh1あ2Ⅳ3sんksnaaksd':
f = open("cogs\Base\data\Quotes.txt")
random_lines = random.choice(f.readlines())
await ctx.send(random_lines)
else:
f = open("cogs\Base\data\Quotes.txt")
for line in f:
if i == int(Qnum):
quote = f.readline()
f.close()
break
else:
i = i+1
await ctx.channel.send(quote)
f.close()
random Qnum是可选参数之类的
假设您的txt文件如下所示:
您可以读取txt文件并使用\n拆分它,如下所示:
为了确保它不会跳过任何行,您可以使用print(array)将带有引号的数组打印到控制台
相关问题 更多 >
编程相关推荐