Skip to content

Commit ab12cdd

Browse files
committed
adding stop and pause features and input validation.
1 parent 0341ac2 commit ab12cdd

File tree

2 files changed

+48
-6
lines changed

2 files changed

+48
-6
lines changed

Pomodoro Timer/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ A simple, customizable command line pomodoro timer built in Python.
55
## Features
66

77
- Customizable work, short and long breaks intervals: Define your own durations for each cycle.
8+
- Pause whenever you want: Pause the timer when you want to take a break.
89
- Cycles: Run Multiple cycles of work and break intervals
910
- Console output: Show work and break intervals in the console.
1011

Pomodoro Timer/main.py

Lines changed: 47 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,28 +4,69 @@
44
def countdown(minutes, label):
55
total_seconds = minutes * 60
66
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}"
99
print(f"{label} Timer: {timer}", end="\r")
1010
time.sleep(1)
1111
total_seconds -= 1
1212
print(f"\n{label} finished!")
1313

1414

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+
1530
def pomodoro_timer(work_min, short_break_min, long_break_min, cycles):
1631
for i in range(cycles):
1732
print(f"\nCycle {i+1} of {cycles}")
1833
countdown(work_min, "Work")
1934
if i < cycles - 1:
35+
print("\nStarting short break...")
36+
if handle_pause_stop():
37+
return
2038
countdown(short_break_min, "Short Break")
2139
else:
40+
print("\nStarting long break...")
41+
if handle_pause_stop():
42+
return
2243
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.")
2364

2465

2566
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: ")
3071

3172
pomodoro_timer(work_minutes, short_break_minutes, long_break_minutes, cycles)

0 commit comments

Comments
 (0)