Created
April 12, 2020 19:34
-
-
Save robertlugg/c6b547d2dbf6a7755bc1b570237a3b10 to your computer and use it in GitHub Desktop.
Python timer example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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