3

在此处输入图像描述

test_count = 0

while test_count <= 100:
    print test_count
    test_count +=1

目前该计数器正在下一行打印,但我正在寻找将其覆盖在“0”上的方法。

4

3 回答 3

6

\r回车符与sys.stdout.flush.

import sys
import time  # for invoking time.sleep(n_seconds) inside loop

counter = 0
while counter <= 100:
    time.sleep(1)
    counter += 1
    sys.stdout.write("\rTesting (%ss elapsed)" % counter)
    sys.stdout.flush()
于 2013-05-07T15:03:24.710 回答
1

Use \r and add a , at the end of your print statement to not automatically write a newline as in the code below.

Also, see the python-progressbar library for some nice text implementations of progress bars.

import time # Added to demonstrate effect

test_count = 0

while test_count <= 100:
    print "\r%3d" % test_count,
    time.sleep(0.1)
    test_count +=1
于 2013-05-07T15:06:38.410 回答
1

您应该包括以下语句: 首先在文件开头导入“os”模块:

import os

然后在循环末尾添加:

os.system('cls')

希望,这对你有帮助:)

于 2013-05-07T15:08:16.947 回答