Welcome to MLink Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
247 views
in Technique[技术] by (71.8m points)

python - Is it possible to print over already printed text? Or to clear already printed text?

I am trying to make a function that will count down from 10 to 1 and only show one number at a time.

ex: after one second the screen reads: 10; Next second it reads: 9 (not 10 9)

Here is my code (without attempted solutions):

def countdown():
    import time
    for i in range(10,0,-1):
        time.sleep(1)
        print(i,end=" ")

I have already tried using ,, and even sys.stdout.write('33[D 33[D') and similar items to that using sys.stdout.write. None of the solutions I have previously found on StackOverflow have worked; giving me outputs such as: 10[D 9[D 8[D etcetera.

I program in python 3.9.0 in IDLE on a mac computer.

(P.S., This countdown function is called after something else that has a print so I do not want to clear the entire screen.)

Thanks in advance to anyone who tries to help!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to MLink Developer Q&A Community for programmer and developer-Open, Learning and Share
...