Skip to content

script_6.py - Countdown Timer

Code

Python
#!/usr/bin/env python3
"""Countdown timer"""

import time

seconds = int(input("Enter countdown seconds: "))

for i in range(seconds, 0, -1):
    print(f"{i}...")
    time.sleep(1)

print("Time's up!")

Explanation

Line 4: import time - import keyword loads a module (library of functions) - time is a built-in Python module for time-related functions - Makes time.sleep() function available - Import statements typically go at top of file

Line 6: seconds = int(input("Enter countdown seconds: ")) - seconds is integer type, stores countdown duration

Line 8: for i in range(seconds, 0, -1): - range() with negative step counts backwards - seconds is start value (e.g., 10) - 0 is stop value (exclusive, so stops at 1) - -1 is step (decrements by 1 each iteration) - If seconds=5, generates: 5, 4, 3, 2, 1 - i decreases each iteration (countdown)

Line 10: time.sleep(1) - .sleep() is a method from time module - Pauses program execution - Argument 1 means 1 second (integer or float) - Creates delay between loop iterations - Makes countdown visible in real-time

Line 12: print("Time's up!") - Not indented, so runs after loop finishes - Executes when countdown reaches 0