0

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 \r,\b, and even sys.stdout.write('\033[D \033[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!

thing1
  • 31
  • 4
  • 2
    In IDLE, you'll never get this to work, but the linked dupe has methods that will work in a 'proper' terminal. – SiHa Dec 21 '20 at 17:19
  • Does this answer your question? [How to overwrite the previous print to stdout in python?](https://stackoverflow.com/questions/5419389/how-to-overwrite-the-previous-print-to-stdout-in-python) – SiHa Dec 21 '20 at 17:19
  • @SiHa what should I use instead of IDLE? – thing1 Dec 21 '20 at 17:21
  • 1
    On a mac just open 'Terminal' on windows it would be CMD – Erick Y. Carreno Dec 21 '20 at 17:23
  • 1
    Just run in a terminal. You can still use IDLE if you like, to write the code, but anything which requires more than just dumping characters to STDOUT will most likely break in IDLE as it's just not designed for that. – SiHa Dec 21 '20 at 17:25
  • https://pythonbasics.org/execute-python-scripts/ – SiHa Dec 21 '20 at 17:25
  • @SiHa so then should i use `\r` or `\b` or something? – thing1 Dec 21 '20 at 17:30
  • @SiHa I tried in terminal it just spammed me with error messages saying syntax error and highlighting the first letter of each line – thing1 Dec 21 '20 at 17:45
  • @SiHa ok I fixed the error messages but now it just prints all at once instead of printing slowly and at intervals – thing1 Dec 22 '20 at 17:39

0 Answers0