goinaction / code

Source Code for Go In Action examples
4.14k stars 2.38k forks source link

Todo-list #120

Open ahmedanwerr1 opened 1 year ago

ahmedanwerr1 commented 1 year ago

import os

def load_todo_list(): """ Load the to-do list from a text file and return a list of tasks. """ filename = input("Enter the name of the to-do list file: ") if os.path.exists(filename): with open(filename, "r") as f: return [line.strip() for line in f] else: return []

def save_todo_list(todo_list): """ Save the to-do list to a text file. """ filename = input("Enter the name of the to-do list file: ") with open(filename, "w") as f: for task in todo_list: f.write(task + "\n")

def add_task(todo_list): """ Prompt the user to enter a new task and add it to the to-do list. """ task = input("Enter a new task: ") todo_list.append(task) print("Task added.")

def create_todo_list(): """ Prompt the user to enter a name for a new to-do list and create a new empty to-do list with that name. """ name = input("Enter a name for the new to-do list: ") return []

def delete_task(todo_list): """ Prompt the user to enter the index of the task to delete and remove it from the to-do list. """ index = int(input("Enter the index of the task to delete: ")) if index < 0 or index >= len(todo_list): print("Invalid index.") else: del todo_list[index] print("Task deleted.")

def list_tasks(todo_list): """ List all tasks in the to-do list. """ print("To-Do List:") for i, task in enumerate(todo_list): print(f"{i}: {task}")

def main(): """ Display a menu of options to the user and perform the selected action. """ todo_list = load_todo_list() while True: print("Menu:") print("1. Add a task to the current to-do list.") print("2. Create a new to-do list.") print("3. Delete a task from the current to-do list.") print("4. List all tasks in the current to-do list.") print("5. Quit the program.") choice = input("Enter your choice: ") if choice == "1": add_task(todo_list) elif choice == "2": todo_list = create_todo_list() elif choice == "3": delete_task(todo_list) elif choice == "4": list_tasks(todo_list) elif choice == "5": save_todo_list(todo_list) print("Goodbye!") break else: print("Invalid choice.")

if name == "main": main()