在同一行发布消息

时间:2018-05-30 02:31:58

标签: python discord discord.py

我的机器人打印出所有猜到的字母和未知字母。我希望将整个语句打印在一行上,但由于我如何使用for char in word(因此检查每个单词是否正确猜到),它会在不同的行上打印每个字符。

for char in word:      
    if char in guesses:   
        await bot.say(char)
    else:
        await bot.say("_")     
        failed = failed + 1 

我如何解决此问题,因为我知道您可以使用sys.stdout.flush()定期打印,但我找不到一种方法来使该属性在我的程序中运行。

编辑: 我通过使用这个代码来实现它,我将字符或_添加到字符串变量然后立即说出来。感谢abccd这个想法!

text = ""
for char in word:      
    if char in guesses:    
        text = text + char
    else:
        text = (text + " - ")
        failed = failed + 1
await bot.say(text)

1 个答案:

答案 0 :(得分:0)

您需要发送连续字符串,因为.say()每次调用时都会发送一条新消息。因此,在致电.say()之前,您需要加入这个词。

text = "".join(c if c in guesses or c.isspace() else "_" for c in word)
failed = text.count("_")

await bot.say(text)

(p.s。我想在刽子手中,你也会显示空格。)

相关问题