0

I am writing a countdown in python but it doesn't work.

for countdown in 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, quit():
    print (countdown)

That's the code. The countdown just zooms by and quits the program. Is there a way to make the countdown comply to real time and countdown and quit in 10 seconds. Not immediately.

Pythonist
  • 11
  • 5

2 Answers2

1

tl;dr python buffers print, and python isn't that slow

import sys, time
for countdown in range(10,1,-1):
    print(countdown)
    sys.stdout.flush() #flushes buffer
    time.sleep(1) #sleep for one second, otherwise loop goes very quickly
print(0)
sys.stdout.flush()
# no quit needed. Program will end when file ends
NightShadeQueen
  • 3,284
  • 3
  • 24
  • 37
1

You need to tell it to wait otherwise it will run the code as fast as your processor can do it.

from time import sleep
for countdown in range(10,0,-1):
  print(countdown)
  sleep(1)
markzz
  • 1,185
  • 11
  • 23