|
4 | 4 | def countdown(minutes, label):
|
5 | 5 | total_seconds = minutes * 60
|
6 | 6 | while total_seconds:
|
7 |
| - minutes, seconds = divmod(total_seconds, 60) |
8 |
| - timer = f"{minutes:02d}:{seconds:02d}" |
| 7 | + mins, secs = divmod(total_seconds, 60) |
| 8 | + timer = f"{mins:02d}:{secs:02d}" |
9 | 9 | print(f"{label} Timer: {timer}", end="\r")
|
10 | 10 | time.sleep(1)
|
11 | 11 | total_seconds -= 1
|
12 | 12 | print(f"\n{label} finished!")
|
13 | 13 |
|
14 | 14 |
|
| 15 | +def handle_pause_stop(): |
| 16 | + while True: |
| 17 | + user_input = input( |
| 18 | + "\nPress 'p' to pause, 's' to stop, or 'Enter' to continue: " |
| 19 | + ).lower() |
| 20 | + if user_input == "p": |
| 21 | + print("Timer paused. Press 'Enter' to resume.") |
| 22 | + input() |
| 23 | + elif user_input == "s": |
| 24 | + print("Timer stopped.") |
| 25 | + return True # Return True to signal that the timer should stop |
| 26 | + else: |
| 27 | + return False # Return False to continue with the timer |
| 28 | + |
| 29 | + |
15 | 30 | def pomodoro_timer(work_min, short_break_min, long_break_min, cycles):
|
16 | 31 | for i in range(cycles):
|
17 | 32 | print(f"\nCycle {i+1} of {cycles}")
|
18 | 33 | countdown(work_min, "Work")
|
19 | 34 | if i < cycles - 1:
|
| 35 | + print("\nStarting short break...") |
| 36 | + if handle_pause_stop(): |
| 37 | + return |
20 | 38 | countdown(short_break_min, "Short Break")
|
21 | 39 | else:
|
| 40 | + print("\nStarting long break...") |
| 41 | + if handle_pause_stop(): |
| 42 | + return |
22 | 43 | countdown(long_break_min, "Long Break")
|
| 44 | + if not repeat_or_end(): |
| 45 | + return |
| 46 | + |
| 47 | + |
| 48 | +def repeat_or_end(): |
| 49 | + user_input = input( |
| 50 | + "\nCycle finished. Would you like to repeat the cycle? (y/n): " |
| 51 | + ).lower() |
| 52 | + return user_input == "y" |
| 53 | + |
| 54 | + |
| 55 | +def get_valid_input(prompt): |
| 56 | + while True: |
| 57 | + try: |
| 58 | + value = int(input(prompt)) |
| 59 | + if value <= 0: |
| 60 | + raise ValueError |
| 61 | + return value |
| 62 | + except ValueError: |
| 63 | + print("Invalid input. Please enter a positive integer.") |
23 | 64 |
|
24 | 65 |
|
25 | 66 | if __name__ == "__main__":
|
26 |
| - work_minutes = int(input("Enter work interval in minutes: ")) |
27 |
| - short_break_minutes = int(input("Enter short break interval in minutes: ")) |
28 |
| - long_break_minutes = int(input("Enter long break interval in minutes: ")) |
29 |
| - cycles = int(input("Enter the number of cycles: ")) |
| 67 | + work_minutes = get_valid_input("Enter work interval in minutes: ") |
| 68 | + short_break_minutes = get_valid_input("Enter short break interval in minutes: ") |
| 69 | + long_break_minutes = get_valid_input("Enter long break interval in minutes: ") |
| 70 | + cycles = get_valid_input("Enter the number of cycles: ") |
30 | 71 |
|
31 | 72 | pomodoro_timer(work_minutes, short_break_minutes, long_break_minutes, cycles)
|
0 commit comments