【发布时间】:2021-01-02 01:42:00
【问题描述】:
我想展示这个系列进行实验。
A
B
AA
BB
AAA
BBB
不添加新行,它只适用于“A”。 这只是 A 的代码,它可以工作。
root@kali-linux:/tmp# cat a.py
import time
x = "A"
while True:
print(x, end = "\r")
x += "A"
time.sleep(1)
现在我添加了 B。
root@kali-linux:/tmp# cat a.py
import time
x = "A"
y = "B"
while True:
print(x, end = "\r")
print(y, end = "\r")
x += "A"
y += "B"
time.sleep(1)
不幸的是,B 吃掉了 A,只有 B 增加了。我尝试过这样的事情,但它会导致我不想要的重复
import time
x = "A"
y = "B"
while True:
print(x, end = "\r")
print('\n', end='\r')
print(y, end = "\r")
x += "A"
y += "B"
time.sleep(1)
有没有什么方法可以不重复打印该系列?我得到了这个答案,但似乎很难在 python3 中实现。
\r moves back to the beginning of the line, it doesn't move to a new line (for that you need \n). When you have 'A' and 'B' it writes all the 'A's and then overwrites it with the 'B's.
You would need to loop through all the 'A's, then print a new line \n, then loop for the 'B's.
编辑
curses 和 coloroma 的答案都可以,但是 curses 在尝试时会导致终端死亡,但它有点无法配置。 Coloroma 是最简单的,也是我需要的答案。
【问题讨论】:
-
我已经根据您的编辑更新了答案。
标签: python python-3.x formatting newline carriage-return