-
Notifications
You must be signed in to change notification settings - Fork 0
/
to do list.txt
98 lines (79 loc) · 3.17 KB
/
to do list.txt
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
Python :To-Do-List
import os
import pickle
from datetime import datetime
class Task:
def __init__(self, description, due_date=None):
self.description = description
self.completed = False
self.created_at = datetime.now()
self.due_date = due_date
class ToDoList:
def __init__(self):
self.tasks = []
def add_task(self, task):
self.tasks.append(task)
def remove_task(self, task):
self.tasks.remove(task)
def show_tasks(self):
for i, task in enumerate(self.tasks, start=1):
print(f"{i}. {task.description} - Created: {task.created_at.strftime('%Y-%m-%d %H:%M:%S')}")
if task.due_date:
print(f" Due: {task.due_date.strftime('%Y-%m-%d')}")
print(f" Status: {'Completed' if task.completed else 'Not Completed'}")
print()
def mark_task_as_completed(self, task):
task.completed = True
def save_to_file(todo_list, filename='todolist.pkl'):
with open(filename, 'wb') as file:
pickle.dump(todo_list, file)
def load_from_file(filename='todolist.pkl'):
if os.path.exists(filename):
with open(filename, 'rb') as file:
return pickle.load(file)
return ToDoList()
def main():
todo_list = load_from_file()
while True:
print("TO-DO LIST")
print("1. Add Task")
print("2. Remove Task")
print("3. Show Tasks")
print("4. Mark Task as Completed")
print("5. Save and Exit")
choice = input("Enter your choice (1-5): ")
if choice == '1':
description = input("Enter task description: ")
due_date_str = input("Enter due date (YYYY-MM-DD), or leave blank for no due date: ")
due_date = datetime.strptime(due_date_str, '%Y-%m-%d') if due_date_str else None
task = Task(description, due_date)
todo_list.add_task(task)
print("Task added successfully!\n")
elif choice == '2':
todo_list.show_tasks()
task_number = int(input("Enter the task number to remove: "))
try:
task = todo_list.tasks[task_number - 1]
todo_list.remove_task(task)
print("Task removed successfully!\n")
except IndexError:
print("Invalid task number. Please try again.\n")
elif choice == '3':
todo_list.show_tasks()
elif choice == '4':
todo_list.show_tasks()
task_number = int(input("Enter the task number to mark as completed: "))
try:
task = todo_list.tasks[task_number - 1]
todo_list.mark_task_as_completed(task)
print("Task marked as completed!\n")
except IndexError:
print("Invalid task number. Please try again.\n")
elif choice == '5':
save_to_file(todo_list)
print("To-Do List saved. Exiting...")
break
else:
print("Invalid choice. Please enter a number between 1 and 5.\n")
if __name__ == "__main__":
main()