-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDailyTask.py
More file actions
80 lines (59 loc) · 2.05 KB
/
Copy pathDailyTask.py
File metadata and controls
80 lines (59 loc) · 2.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
from datetime import datetime
def log_task():
"""Log a new task with timestamp"""
task = input("Enter task description: ")
if not task.strip():
print("Task cannot be empty!")
return
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
log_entry = f"[{timestamp}] {task}\n"
with open("log.txt", "a") as f:
f.write(log_entry)
print(f"✓ Task logged: {task}")
def view_last_tasks(n=5):
"""Display last N tasks from log file"""
try:
with open("log.txt", "r") as f:
lines = f.readlines()
if not lines:
print("No tasks logged yet!")
return
# Get last N lines (or all if less than N)
last_tasks = lines[-n:]
print(f"\n{'='*50}")
print(f"Last {len(last_tasks)} task(s):")
print('='*50)
for i, task in enumerate(reversed(last_tasks), 1):
print(f"{i}. {task.strip()}")
print('='*50)
except FileNotFoundError:
print("No tasks logged yet!")
def main():
"""Main program loop"""
print("=" * 50)
print("DAILY TASK LOGGER".center(50))
print("=" * 50)
while True:
print("\n📋 Menu:")
print("1. Log a task")
print("2. View last 5 tasks")
print("3. Quit")
print("-" * 50)
try:
choice = input("Choose option (1-3): ").strip()
if choice == '1':
log_task()
elif choice == '2':
view_last_tasks(5)
elif choice == '3':
print("\n👋 Thanks for using Task Logger!")
break
else:
print("⚠️ Invalid option! Please choose 1, 2, or 3")
except KeyboardInterrupt:
print("\n\n👋 Interrupted! Goodbye!")
break
except Exception as e:
print(f"⚠️ An error occurred: {e}")
if __name__ == "__main__":
main()