Skip to content

Commit 9f67654

Browse files
authored
Create Digital-Clock.md
1 parent b74de7d commit 9f67654

1 file changed

Lines changed: 41 additions & 0 deletions

File tree

Digital-Clock.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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()

0 commit comments

Comments
 (0)