如何在python中删除最后打印的行? [英] How can I remove last printed line in python?

查看:75
本文介绍了如何在python中删除最后打印的行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用python制作倒计时程序.我想把它变成它,删除最后打印的行,所以我可以打印新的第二行.

I'm trying to make countdown program with python. I want to turn that into it removes last printed line, so i can print new second.

import time

def countdown():
    minute = 60
    while minute >= 0:
        m, s = divmod(minute, 60)
        time_left = str(m).zfill(2) + ':' + str(s).zfill(2)
        print(time_left)
        time.sleep(1) 
        minute -= 1

countdown()

我正在Raspberry Pi上运行python 2.7.13.

I am running python 2.7.13 on Raspberry Pi.

推荐答案

尝试以下操作(它是在python2中制成的):

Try the following (it's made in python2):

import time, sys

def countdown(totalTime):
    try:
        while totalTime >= 0:
            mins, secs = divmod(totalTime, 60)
            sys.stdout.write("\rWaiting for {:02d}:{:02d}  minutes...".format(mins, secs))
            sys.stdout.flush()
            time.sleep(1)
            totalTime -= 1
            if totalTime <= -1:
                print "\n"
                break
    except KeyboardInterrupt:
        exit("\n^C Detected!\nExiting...")

这样称呼它:倒数计时(时间)例如:倒数(600)10分钟.

Call it like this: countdown(time) For example: countdown(600) for 10 minutes.

这篇关于如何在python中删除最后打印的行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆