在python中打印时随机播放文本

问题描述

我已经编写了一个简单的python程序

import sys
from time import sleep

def main():
   for i in range(11):
      sys.stdout.write("The value is %d"%int(i))
      sleep(1)
      sys.stdout.flush()
      
if __name__=="__main__":
   main()

它给我以下输出,每个循环之间的延迟为1s

The value is 0The value is 1The value is 2The value is 3The value is 4The value is 5The value is 6The value is 7The value is 8The value is 9The value is 10

但是我需要这样的输出

This is 1

1s之后,应在同一位置将1替换为2 如果您不了解我的情况,请在https://github.com/azan121468/wordlist_gen上查看我的单词表生成器 我想在我的简单程序中应用同样的东西,但是我做不到 请 帮助

解决方法

可以使用退格('\b')字符:

from time import sleep


def main():
    print('The value is 0',end='')
    for i in range(1,11):
        print('\b' + str(i),end='')
        sleep(1)


if __name__ == "__main__":
    main()

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...