【问题标题】:How to output message in Python and then delete it in output [duplicate]如何在Python中输出消息然后在输出中删除它[重复]
【发布时间】:2019-11-25 14:45:24
【问题描述】:

所以我想输出'|'然后将其删除并粘贴“/”(在一行中),以便制作动画。

import time


animation = '|'
print(animation)

time.sleep(0.5)

animation = animation.replace('|', '/')
print(animation)

【问题讨论】:

标签: python python-3.x


【解决方案1】:

简而言之,需要输出\r字符,即carriage RETURN command。它将插入符号放在控制台中的行首。下一个输出基本会改写行内容。

import sys, time

i = 0
while True:
    sys.stdout.write('/-\\|'[i%4])
    time.sleep(0.5)
    sys.stdout.write('\r')
    i += 1

【讨论】:

    猜你喜欢
    • 2022-01-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-13
    • 2023-03-03
    • 2015-09-26
    • 1970-01-01
    • 2017-10-19
    相关资源
    最近更新 更多