python todo app
- User
python todo app
- Assistant
python todo app
Creating a simple to-do application in Python can be a great way to practice your coding skills. Below is a step-by-step guide to create a basic command-line to-do application.
First, let's start by defining what features our to-do app will include:
Here's a simple implementation:
import os
import json
# Define the file name to store tasks
FILENAME = 'tasks.json'
# Load tasks from the file if it exists
def load_tasks():
if os.path.exists(FILENAME):
with open(FILENAME, 'r') as file:
return json.load(file)
return []
# Save tasks to the file
def save_tasks(tasks):
with open(FILENAME, 'w') as file:
json.dump(tasks, file)
def display_tasks(tasks):
if not tasks:
print("No tasks to display.")
return
for i, task in enumerate(tasks):
status = "Completed" if task['completed'] else "Pending"
print(f"{i + 1}. {task['description']} [{status}]")
def add_task(tasks):
description = input("Enter task description: ")
tasks.append({'description': description, 'completed': False})
print("Task added.")
def mark_task_completed(tasks):
display_tasks(tasks)
task_number = int(input("Enter task number to mark completed: "))
if 0 < task_number <= len(tasks):
tasks[task_number - 1]['completed'] = True
print("Task marked as completed.")
else:
print("Invalid task number.")
def delete_task(tasks):
display_tasks(tasks)
task_number = int(input("Enter task number to delete: "))
if 0 < task_number <= len(tasks):
tasks.pop(task_number - 1)
print("Task deleted.")
else:
print("Invalid task number.")
def main():
tasks = load_tasks()
while True:
print("\nTo-Do List App:")
print("1. View tasks")
print("2. Add task")
print("3. Mark task as completed")
print("4. Delete task")
print("5. Exit")
choice = input("Enter your choice: ")
if choice == '1':
display_tasks(tasks)
elif choice == '2':
add_task(tasks)
elif choice == '3':
mark_task_completed(tasks)
elif choice == '4':
delete_task(tasks)
elif choice == '5':
save_tasks(tasks)
print("Goodbye!")
break
else:
print("Invalid choice. Please try again.")
if __name__ == "__main__":
main()
Imports and File Handling:
os module is used to check if the file exists.json module is used for saving and loading tasks in a JSON format.FILENAME is the name of the file where tasks will be stored.Loading and Saving Tasks:
load_tasks reads tasks from a JSON file if it exists; otherwise, it returns an empty list.save_tasks writes the tasks to a JSON file.Displaying Tasks:
display_tasks prints the list of tasks with their status (completed or pending).Adding a Task:
add_task allows the user to input a new task description, which is then appended to the task list.Mark Task as Completed:
mark_task_completed displays the list of tasks, and the user can mark a specific task as completed.Deleting a Task:
delete_task displays the list of tasks, and the user can delete a specific task.Main Loop:
main function runs the app, providing a menu for the user to choose an action. The loop continues until the user chooses to exit.Save this script as todo_app.py, and run it. This basic app will allow you to manage your tasks through a simple command-line interface.
Feel free to enhance this base version with additional features or a graphical user interface (GUI) if you're interested in expanding your project further!
Not yet included in a published release.