社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  Python

如何在Python中覆盖打印的单行?

Ayush Gupta • 5 年前 • 918 次点击  

代码是:

import random , time
while True:
    for i in range(6):
        print(random.randint(0,9), end=" ")
    time.sleep(2)
    print(" ")

Output1
Output2
etc.

我希望Output2覆盖Output1 谢谢!

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/54582
 
918 次点击  
文章 [ 1 ]  |  最新文章 5 年前
Gino Mempin
Reply   •   1 楼
Gino Mempin    5 年前

你只需要打印 end='\r' (回车)。

while True:
    for i in range(6):
        print(random.randint(0, 9), end=" ")
    time.sleep(2)
    print(end="\r")

print 使用 end='\n' (新行)作为默认值。通过将其更改为回车,可以强制它将光标倒回行的开头。只要下一行的打印长度相同,它就会覆盖上一行。