Chapter 20: Countdown Timerยถ
๐ Open Notebookยถ
๐บ Video Tutorialยถ
Countdown timer program in Python โ (8:59)
๐ What Youโll Learnยถ
Build a real countdown timer using Pythonโs time module - combining loops, time operations, and user input!
๐ฏ Learning Objectivesยถ
Use the time module for delays
Convert between time units (hours/minutes/seconds)
Create countdown logic
Format time display (HH:MM:SS)
Build an interactive timer program
Clear console output for better display
๐ Concept Explanationยถ
What is the Time Module?ยถ
Pythonโs time module provides time-related functions like delays, current time, and time measurements.
Key Functionsยถ
import time
time.sleep(seconds) # Pause execution
time.time() # Current time in seconds since epoch
Time Conversionยถ
1 hour = 60 minutes = 3600 seconds
1 minute = 60 seconds
Timer Logicยถ
Get total time in seconds
While time > 0:
Display remaining time
Wait 1 second
Subtract 1 from time
Show completion message
๐ก Examplesยถ
Example 1: Basic Sleepยถ
import time
print("Starting...")
time.sleep(3) # Wait 3 seconds
print("3 seconds have passed!")
Example 2: Simple Countdownยถ
import time
for i in range(5, 0, -1):
print(i)
time.sleep(1)
print("Time's up!")
Example 3: Seconds to Time Formatยถ
def format_time(seconds):
hours = seconds // 3600
minutes = (seconds % 3600) // 60
secs = seconds % 60
return f"{hours:02d}:{minutes:02d}:{secs:02d}"
# Examples
print(format_time(3661)) # 01:01:01
print(format_time(125)) # 00:02:05
print(format_time(7200)) # 02:00:00
Example 4: Time Input Converterยถ
hours = int(input("Hours: "))
minutes = int(input("Minutes: "))
seconds = int(input("Seconds: "))
total_seconds = (hours * 3600) + (minutes * 60) + seconds
print(f"Total: {total_seconds} seconds")
Example 5: Basic Countdown Timerยถ
import time
def countdown(seconds):
while seconds > 0:
mins = seconds // 60
secs = seconds % 60
timer = f"{mins:02d}:{secs:02d}"
print(timer, end="\r") # \r returns cursor to start
time.sleep(1)
seconds -= 1
print("Time's up! ")
countdown(10)
Example 6: Full Featured Timerยถ
import time
print("=== Countdown Timer ===\n")
hours = int(input("Enter hours: "))
minutes = int(input("Enter minutes: "))
seconds = int(input("Enter seconds: "))
total_seconds = (hours * 3600) + (minutes * 60) + seconds
print(f"\nTimer set for: {hours:02d}:{minutes:02d}:{seconds:02d}")
print("Starting countdown...\n")
while total_seconds > 0:
hrs = total_seconds // 3600
mins = (total_seconds % 3600) // 60
secs = total_seconds % 60
print(f"{hrs:02d}:{mins:02d}:{secs:02d}", end="\r")
time.sleep(1)
total_seconds -= 1
print("๐ Time's up! ")
Example 7: Pomodoro Timerยถ
import time
def pomodoro_session(work_mins, break_mins):
# Work session
print(f"Work time: {work_mins} minutes")
countdown_minutes(work_mins)
print("โ
Work session complete!")
# Break session
print(f"\nBreak time: {break_mins} minutes")
countdown_minutes(break_mins)
print("โ
Break complete!")
def countdown_minutes(minutes):
seconds = minutes * 60
while seconds > 0:
mins = seconds // 60
secs = seconds % 60
print(f"{mins:02d}:{secs:02d}", end="\r")
time.sleep(1)
seconds -= 1
print()
# 25 min work, 5 min break
pomodoro_session(25, 5)
Example 8: Multiple Timersยถ
import time
def run_timer(name, seconds):
print(f"\n{name}: {seconds} seconds")
for i in range(seconds, 0, -1):
print(f"{name}: {i}s remaining", end="\r")
time.sleep(1)
print(f"{name}: Complete! ")
# Cooking timers
run_timer("Pasta", 10)
run_timer("Sauce", 5)
run_timer("Garlic Bread", 8)
print("\n๐ Dinner is ready!")
Example 9: Progress Bar Timerยถ
import time
def timer_with_progress(seconds):
total = seconds
for remaining in range(seconds, 0, -1):
progress = (total - remaining) / total
bar_length = 30
filled = int(bar_length * progress)
bar = "โ" * filled + "โ" * (bar_length - filled)
mins = remaining // 60
secs = remaining % 60
print(f"[{bar}] {mins:02d}:{secs:02d}", end="\r")
time.sleep(1)
print(f"[{'โ' * bar_length}] Complete! ")
timer_with_progress(15)
Example 10: Interval Timerยถ
import time
def interval_timer(work_sec, rest_sec, rounds):
for round_num in range(1, rounds + 1):
print(f"\nRound {round_num}/{rounds}")
# Work interval
print("WORK!", end=" ")
for i in range(work_sec, 0, -1):
print(f"{i}...", end=" ", flush=True)
time.sleep(1)
if round_num < rounds:
# Rest interval
print("\nREST!", end=" ")
for i in range(rest_sec, 0, -1):
print(f"{i}...", end=" ", flush=True)
time.sleep(1)
print("\n\nโ
Workout complete!")
# 20 seconds work, 10 seconds rest, 3 rounds
interval_timer(20, 10, 3)
๐ Common Mistakesยถ
Mistake 1: Not Importing time Moduleยถ
# Wrong
print("Wait 1 second...")
sleep(1) # NameError!
# Correct
import time
print("Wait 1 second...")
time.sleep(1)
Mistake 2: Wrong Time Conversionยถ
# Wrong
hours = 2
minutes = 30
total = hours + minutes # = 2.5 (wrong!)
# Correct
total_seconds = (hours * 3600) + (minutes * 60) # = 9000
Mistake 3: Display Format Issuesยถ
# Poor formatting
mins = 5
secs = 3
print(f"{mins}:{secs}") # 5:3
# Better formatting
print(f"{mins:02d}:{secs:02d}") # 05:03
Mistake 4: Infinite Loopยถ
# Wrong - never decrements
seconds = 10
while seconds > 0:
print(seconds)
time.sleep(1)
# Forgot: seconds -= 1
# Correct
seconds = 10
while seconds > 0:
print(seconds)
time.sleep(1)
seconds -= 1
Mistake 5: Not Clearing Previous Outputยถ
# Messy output
for i in range(5, 0, -1):
print(f"Time: {i}")
time.sleep(1)
# Prints 5 lines
# Cleaner output
for i in range(5, 0, -1):
print(f"Time: {i}", end="\r")
time.sleep(1)
# Updates same line
โ๏ธ Practice Exercisesยถ
Easyยถ
Create a 10-second countdown
Make a 5-minute timer
Convert 90 seconds to MM:SS format
Build a simple stopwatch
Create a โtake a breakโ reminder (every 20 minutes)
Mediumยถ
Build a kitchen timer with multiple alarms
Create a workout interval timer (30s work, 15s rest)
Make a Pomodoro timer (25 min work, 5 min break)
Build a countdown with pause/resume
Create a timer that accepts โ5m 30sโ input format
Hardยถ
Multi-timer manager (run multiple timers)
Timer with sound notification
Exam timer with warnings at intervals
Build a timer with progress visualization
Create a competitive countdown (2 players)
๐ฎ Real-World Applicationsยถ
Application 1: Study Timerยถ
import time
def study_session():
print("=== Study Timer ===\n")
study_time = int(input("Study duration (minutes): "))
break_time = int(input("Break duration (minutes): "))
sessions = int(input("Number of sessions: "))
for session in range(1, sessions + 1):
print(f"\n๐ Study Session {session}/{sessions}")
countdown_min(study_time)
if session < sessions:
print(f"\nโ Break Time!")
countdown_min(break_time)
print("\nโ
Study session complete!")
def countdown_min(minutes):
seconds = minutes * 60
while seconds > 0:
m = seconds // 60
s = seconds % 60
print(f"{m:02d}:{s:02d}", end="\r")
time.sleep(1)
seconds -= 1
study_session()
Application 2: Cooking Assistantยถ
import time
def cooking_timer():
print("=== Cooking Timer Assistant ===\n")
tasks = []
num_tasks = int(input("How many cooking tasks? "))
for i in range(num_tasks):
name = input(f"\nTask {i+1} name: ")
minutes = int(input(f"Minutes for {name}: "))
tasks.append((name, minutes * 60))
print("\n๐ณ Starting cooking timers...\n")
for name, seconds in tasks:
print(f"\nโฐ {name} - {seconds//60} minutes")
while seconds > 0:
m = seconds // 60
s = seconds % 60
print(f"{name}: {m:02d}:{s:02d}", end="\r")
time.sleep(1)
seconds -= 1
print(f"โ
{name} is ready! ")
print("\n๐ฝ๏ธ All cooking complete!")
cooking_timer()
Application 3: Fitness Interval Timerยถ
import time
def hiit_workout():
print("=== HIIT Workout Timer ===\n")
work_time = int(input("Work interval (seconds): "))
rest_time = int(input("Rest interval (seconds): "))
rounds = int(input("Number of rounds: "))
print("\nGet ready!")
time.sleep(3)
for round_num in range(1, rounds + 1):
# Work phase
print(f"\n๐ฅ ROUND {round_num}/{rounds} - WORK!")
for i in range(work_time, 0, -1):
print(f"โฑ๏ธ {i} seconds", end="\r")
time.sleep(1)
# Rest phase (except last round)
if round_num < rounds:
print(f"\n๐ REST")
for i in range(rest_time, 0, -1):
print(f"โฑ๏ธ {i} seconds", end="\r")
time.sleep(1)
print("\n\n๐ WORKOUT COMPLETE!")
hiit_workout()
Application 4: Presentation Timerยถ
import time
def presentation_timer():
print("=== Presentation Timer ===\n")
total_minutes = int(input("Presentation length (minutes): "))
warning_minutes = int(input("Warning at (minutes remaining): "))
total_seconds = total_minutes * 60
warning_seconds = warning_minutes * 60
print(f"\nTimer: {total_minutes} minutes")
print("Starting...\n")
while total_seconds > 0:
mins = total_seconds // 60
secs = total_seconds % 60
# Warnings
if total_seconds == warning_seconds:
print(f"\nโ ๏ธ {warning_minutes} minutes remaining!\n")
elif total_seconds == 60:
print(f"\nโ ๏ธ 1 minute remaining!\n")
elif total_seconds == 30:
print(f"\nโ ๏ธ 30 seconds!\n")
print(f"โฑ๏ธ {mins:02d}:{secs:02d}", end="\r")
time.sleep(1)
total_seconds -= 1
print("\n\nโฐ Time's up! ")
presentation_timer()
๐ Challenge Projectsยถ
Challenge 1: Advanced Pomodoroยถ
Create a full Pomodoro timer with:
25 min work / 5 min short break
15 min long break after 4 sessions
Track completed sessions
Show productivity statistics
Save session history to file
Challenge 2: Multi-Timer Dashboardยถ
Build a program that:
Runs multiple timers simultaneously
Shows all timers in real-time
Allows adding/removing timers
Prioritizes by urgency
Alerts when each timer completes
Challenge 3: Exam Timerยถ
Create an exam timer with:
Total time display
Time per question calculator
Warnings at 50%, 25%, 10% remaining
Pause/resume functionality
Extra time addition
Challenge 4: Time Trackerยถ
Build a productivity tracker:
Start/stop timer for tasks
Categorize tasks (work, study, break)
Show daily summary
Calculate productivity percentage
Export report
Challenge 5: Countdown Event Timerยถ
Create a timer for future events:
Calculate days, hours, mins, secs until event
Update in real-time
Multiple event tracking
Show percentage complete
Milestone notifications
๐ Time Module Referenceยถ
import time
# Pause execution
time.sleep(seconds)
# Current time (seconds since epoch)
current = time.time()
# Format time for display
time.strftime("%H:%M:%S")
# Measure elapsed time
start = time.time()
# ... code ...
elapsed = time.time() - start
๐จ Display Formatting Tricksยถ
# Overwrite same line
print("Loading...", end="\r")
# Clear to end of line
print("Done! ", end="\r")
# Flush output immediately
print("Progress", flush=True)
# Format with leading zeros
print(f"{5:02d}") # "05"
print(f"{12:02d}") # "12"
๐ช Try It Yourselfยถ
Modify main.py to:
Add hours support
Show progress bar
Add pause/resume feature
Include sound notification
Save timer history
Add multiple preset timers
๐ Key Takeaways from Videoยถ
Functions are reusable blocks of code
Import modules to use external code
Use loops to repeat actions
๐ก These points cover the main concepts from the video tutorial to help reinforce your learning.
๐ Next Chapterยถ
Continue to Chapter 20: Nested Loops to learn about loops within loops!