class TodoList: def __init__(self): self.tasks = {} def add_task(self, task): self.tasks[task] = "Pending" def delete_task(self, task): if task in self.tasks: del self.tasks[task] else: print("Task not found.") def mark_task_completed(self, task): if task in self.tasks: self.tasks[task] = "Completed" else: print("Task not found.") def view_tasks(self): if self.tasks: print("To-Do List:") for task, status in self.tasks.items(): print(f"{task} - {status}") else: print("No tasks in the list.") def main(): todo_list = TodoList() while True: print("\n1. Add Task") print("2. Delete Task") print("3. Mark Task as Completed") print("4. View Tasks") print("5. Exit") choice = input("Enter your choice: ") if choice == '1': task = input("Enter task: ") todo_list.add_task(task) elif choice == '2': task = input("Enter task to delete: ") todo_list.delete_task(task) elif choice == '3': task = input("Enter task to mark as completed: ") todo_list.mark_task_completed(task) elif choice == '4': todo_list.view_tasks() elif choice == '5': print("Exiting...") break else: print("Invalid choice. Please try again.") if __name__ == "__main__": main()
Out put
Certainly! Let's break down the code step by step:
TodoList Class:
- The
TodoList
class is defined to represent a to-do list. It contains the following methods:__init__()
: This is the constructor method that initializes thetasks
dictionary, which will store tasks and their corresponding status (completed or pending).add_task(task)
: This method adds a new task to the to-do list. It takes a task as input and adds it to thetasks
dictionary with the status set to "Pending".delete_task(task)
: This method removes a task from the to-do list. It takes a task as input and removes it from thetasks
dictionary if it exists.mark_task_completed(task)
: This method marks a task as completed. It takes a task as input and updates its status to "Completed" in thetasks
dictionary.view_tasks()
: This method displays all tasks in the to-do list along with their status.
- The
Main Function (
main()
):- The
main()
function is the entry point of the program. It creates an instance of theTodoList
class and provides a menu for users to interact with the to-do list. - Inside the
while
loop, the user is prompted to choose from a set of options:- '1': Add Task - Prompts the user to enter a task and adds it to the to-do list.
- '2': Delete Task - Prompts the user to enter a task to delete and removes it from the to-do list.
- '3': Mark Task as Completed - Prompts the user to enter a task to mark as completed and updates its status.
- '4': View Tasks - Displays all tasks in the to-do list.
- '5': Exit - Exits the program.
- If the user enters an invalid choice, an error message is displayed, and the menu is displayed again.
- The
Execution:
- The
main()
function is called at the end of the script usingif __name__ == "__main__":
to ensure that the code insidemain()
is executed only when the script is run directly, not when it is imported as a module into another script.
- The
Overall, this code provides a simple command-line interface for users to manage their to-do list by adding, deleting, marking tasks as completed, and viewing tasks. The to-do list itself is represented internally using a dictionary, where tasks are stored as keys and their status as values.
No comments:
Post a Comment