Skip to content

Instantly share code, notes, and snippets.

@robertlugg
Created April 12, 2020 19:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save robertlugg/c6b547d2dbf6a7755bc1b570237a3b10 to your computer and use it in GitHub Desktop.
Save robertlugg/c6b547d2dbf6a7755bc1b570237a3b10 to your computer and use it in GitHub Desktop.
Python timer example
import time
while True:
print("How long or 'exit' when finished")
try:
uin = input(">> ")
except Exception:
break
if uin == 'exit':
break
try:
when_to_stop = abs(int(uin))
except ValueError:
print("Please re-enter number of seconds.")
continue
time_so_far = 0
try:
while time_so_far <= when_to_stop:
time_left = when_to_stop - time_so_far
print(f'{time_left}\r', end='')
time.sleep(1.0)
time_so_far += 1
except KeyboardInterrupt:
print('\nTimer aborted')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment