File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ """
2+ Digital Clock (Command-Line Version)
3+ -----------------------------------------
4+ This simple program displays the current time
5+ and updates every second like a live clock.
6+
7+ Concepts used:
8+ - Python's time module
9+ - Infinite loops
10+ - String formatting
11+ - Real-time screen updates using '\r' (carriage return)
12+ """
13+
14+ import time # Provides time-related functions like sleep() and localtime()
15+
16+ def digital_clock():
17+ """Displays the current time continuously until the user stops the program."""
18+ print("⏰ Digital Clock Started! (Press Ctrl+C to stop)\n")
19+
20+ try:
21+ while True:
22+ # Get the current local time as a struct_time object
23+ current_time = time.localtime()
24+
25+ # Format time as HH:MM:SS using strftime()
26+ formatted_time = time.strftime("%H:%M:%S", current_time)
27+
28+ # Print time on the same line using '\r' to overwrite previous output
29+ print(f"\r🕒 Current Time: {formatted_time}", end="")
30+
31+ # Wait for 1 second before updating again
32+ time.sleep(1)
33+
34+ except KeyboardInterrupt:
35+ # Graceful exit message when user presses Ctrl+C
36+ print("\n🛑 Clock stopped by user. Goodbye!")
37+
38+
39+ # Entry point of the program
40+ if __ name__ == "__ main__ ":
41+ digital_clock()
You can’t perform that action at this time.
0 commit comments