-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.py
More file actions
130 lines (108 loc) · 3.81 KB
/
Copy pathmain.py
File metadata and controls
130 lines (108 loc) · 3.81 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# Required Modules
from datetime import datetime
# Create an empty tasks list
tasks = []
# Create or open the tasks file
try:
x = open("tasks.txt", 'x')
x.close()
except FileExistsError:
pass
# Function to save tasks to text file
def save_tasks():
with open("tasks.txt", 'w') as file:
for task in tasks:
file.write(f"Title: {task['title']}\n")
file.write(f"Description: {task['description']}\n")
file.write(f"Due Date: {task['due_date']}\n")
file.write(f"Status: {'completed' if task['completed'] else 'pending'}\n\n")
# Function to load tasks from text file
def load_tasks():
try:
with open("tasks.txt", 'r') as file:
lines = file.readlines()
task_info = {"title": "", "description": "", "due_date": "", "completed": False}
for line in lines:
line = line.strip()
if line.startswith("Title: "):
task_info["title"] = line[len("Title: "):]
elif line.startswith("Description: "):
task_info["description"] = line[len("Description: "):]
elif line.startswith("Due Date: "):
task_info["due_date"] = line[len("Due Date: "):]
elif line.startswith("Status: "):
task_info["completed"] = (line[len("Status: "):] == "completed")
elif not line:
tasks.append(task_info)
task_info = {"title": "", "description": "", "due_date": "", "completed": False}
except FileNotFoundError:
pass
# List_tasks function
def list_tasks():
if not tasks:
print("No tasks found.")
else:
for index, task in enumerate(tasks, 1):
print(f"{index}. Title: {task['title']}")
print(f" Description: {task['description']}")
print(f" Due Date: {task['due_date']}")
print(f" Status: {'Completed' if task['completed'] else 'Pending'}")
# Function to add task
def add_task():
title = input("Enter task title: ")
description = input("Enter task description: ")
due_date = input("Enter due date (DD-MM-YYYY): ")
try:
due_date = datetime.strptime(due_date, "%d-%m-%Y")
except ValueError:
print("Invalid date format. Use DD-MM-YYYY.")
return
task = {
"title": title,
"description": description,
"due_date": due_date.strftime("%d-%m-%Y"),
"completed": False
}
tasks.append(task)
save_tasks()
print("Task added successfully!")
# function to mark the task as completed
def mark_completed():
list_tasks()
try:
task_index = int(input("Enter the index of the task that you want to mark as completed: "))
if 1 <= task_index <= len(tasks):
tasks[task_index - 1]["completed"] = True
save_tasks()
print("Task Marked as Completed ✅")
else:
print("Enter a valid task index.")
except ValueError:
print("Invalid Input. Please enter a valid number")
# main function
def main():
load_tasks()
while True:
print("\nTask Manager Menu:")
print("1. List Tasks")
print("2. Add Task")
print("3. Mark Task as Completed")
print("4. Quit")
try:
choice = int(input("Enter your choice: "))
if choice == 1:
list_tasks()
elif choice == 2:
add_task()
elif choice == 3:
mark_completed()
elif choice == 4:
print("Goodbye!")
break
else:
print("Invalid choice. Please try again.")
except ValueError:
print("Invalid input. Please enter a valid number.")
# run program
if __name__ == "__main__":
main()