Py学习  »  Python

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

Ayush Gupta • 4 年前 • 819 次点击  

代码是:

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
 
819 次点击  
文章 [ 1 ]  |  最新文章 4 年前
Gino Mempin
Reply   •   1 楼
Gino Mempin    4 年前

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

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

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