You are on page 1of 1

4/8/2020 Pastebin.com - Printed Paste ID: https://pastebin.

com/ziU7BaL9

1. import time
2. import datetime
3.
4. def countdown_timer(x, now=datetime.datetime.now):
5. target = now()
6. one_second_later = datetime.timedelta(seconds=1)
7. for remaining in range(x, 0, -1):
8. target += one_second_later
9. print('\r', datetime.timedelta(seconds=remaining), 'remaining', end='\r')
10. time.sleep((target - now()).total_seconds())
11. print('\nTIMER ended')
12.
13. def get_input_format(supported_formats=('hrs', 'min', 'sec')):
14. while True:
15. units = input('Enter the format in which you want to time yourself {}:
'.format(supported_formats))
16. if units in supported_formats:
17. return units
18. print('Invalid input. Please re-enter your desired format')
19. print('The input is case sensitive')
20.
21. def main(unit_input):
22. hours = minutes = seconds = 0
23. while True:
24. try:
25. if unit_input == 'hrs':
26. hours = int(input('Enter the number of hours: '))
27. if unit_input != 'sec':
28. minutes = int(input('Enter the number of minutes: '))
29. seconds = int(input('Enter the number of seconds: '))
30. except ValueError:
31. print('Invalid Input. Re-enter the values')
32. else:
33. break
34. delay = datetime.timedelta(hours=hours, minutes=minutes, seconds=seconds)
35. print('Starting countdown for {}'.format(delay))
36. countdown_timer(int(delay.total_seconds()))
37.
38. if __name__ == '__main__':
39. main(get_input_format())

https://pastebin.com/print/ziU7BaL9 1/1

You might also like