想在定时器程序 (python) 中清除终端上的前一行?

Want to clear the previous line on terminal in a timer program (python)?

所以我有一个计时器的代码,当有人输入数字时,我只想启动计时器并且输入的数字不是 visible.Code 就像

s=int(input("enter time in sec"))
countdown(s)

所以输出是:

enter time in sec 5
0:4
0:3
0:2
0:1
0:0
time up

我想要的是首先删除“在第 5 节中输入时间”,然后当 0:4 打印时我想在不低于它的位置打印 0:3。

我尝试了 并将其复制粘贴到代码中,就像这样

s = int(input("enter time in sec "))
print ('3[1A3[K')
countdown(s)

似乎什么也没有发生,如果我在实施中有误或没有奏效,请不要这样做。

编辑:-

两种都试过了

os.system('cls')

print ('3[1A3[K')

似乎都不起作用

我的代码,

 def time_format(inp):
          import os
          m,s=divmod(inp,60)
          #os.system('cls')
          print ('3[1A3[K')
          ...code for printing time below...

编辑:- 我在 windows 并且正在使用 Idle。
两者都不起作用

查看已接受的答案:How to clear the interpreter console?

os.system('cls') 适用于 Windows,而 os.system('clear') 适用于 Linux

您没有指定 OS 您使用的是什么,但如果您定位 Linux 它可以 是:

#!/usr/bin/env python3

import time


def countdown(seconds):
    for i in range(seconds, -1, -1):
        # move to the beginning of the line and remove line
        print("\r3[K", end='', flush=True)
        print(f"\r{i}", end='', flush=True)
        time.sleep(1)
    print("\nTime's up!")


s = int(input("enter time in sec: "))
# move one line up
print("3[1A", end='', flush=True)
countdown(s)

它是这样工作的:

收到一条流行消息说我不应该删除已回答的问题,因此我要自己回答。 我相信

print("3[1A", end='', flush=True)

os.system('cls')

有效。问题是没有 option/method 可以在 IDLE 上执行此操作。因为如果我双击并 运行 文件原样但 none 在 IDLE

上工作,我尝试了这两种方法