Terminal ထဲမှာ run လုပ်တဲ့ Todo app တစ်ခုရေးမယ်။ Add, list, mark done, delete လုပ်နိုင်မယ်။ Data ကို todos.json ထဲသိမ်းမယ်။
import json
from pathlib import Path
DATA_FILE = Path("todos.json")
def load_todos():
if DATA_FILE.exists():
with open(DATA_FILE, "r", encoding="utf-8") as file:
return json.load(file)
return []
def save_todos(todos):
with open(DATA_FILE, "w", encoding="utf-8") as file:
json.dump(todos, file, indent=2)
def show_todos(todos):
if not todos:
print("No todos yet.")
return
for index, todo in enumerate(todos, start=1):
status = "Done" if todo["done"] else "Pending"
print(f"{index}. [{status}] {todo['title']}")
def add_todo(todos):
title = input("Todo title: ").strip()
if title:
todos.append({"title": title, "done": False})
save_todos(todos)
print("Todo added.")
else:
print("Title cannot be empty.")
def mark_done(todos):
show_todos(todos)
try:
number = int(input("Done number: "))
todos[number - 1]["done"] = True
save_todos(todos)
print("Marked as done.")
except (ValueError, IndexError):
print("Invalid number.")
def delete_todo(todos):
show_todos(todos)
try:
number = int(input("Delete number: "))
removed = todos.pop(number - 1)
save_todos(todos)
print(f"Deleted: {removed['title']}")
except (ValueError, IndexError):
print("Invalid number.")
def main():
todos = load_todos()
while True:
print("\n--- Todo App ---")
print("1. List todos")
print("2. Add todo")
print("3. Mark done")
print("4. Delete todo")
print("5. Exit")
choice = input("Choose: ")
if choice == "1":
show_todos(todos)
elif choice == "2":
add_todo(todos)
elif choice == "3":
mark_done(todos)
elif choice == "4":
delete_todo(todos)
elif choice == "5":
print("Bye!")
break
else:
print("Invalid choice.")
main()load_todos() က JSON file ရှိရင်ဖတ်တယ်။ မရှိရင် empty list ပြန်ပေးတယ်။ save_todos() က list ကို JSON အဖြစ်သိမ်းတယ်။ Menu loop က user ရွေးတဲ့ action အလိုက် function ခေါ်တယ်။
1. Todo တစ်ခုချင်းစီမှာ created_at date ထည့်ပါ 2. Pending only / Done only filter ထည့်ပါ 3. Search todo by keyword ထည့်ပါ 4. Priority: low / medium / high ထည့်ပါ 5. OOP version ပြန်ရေးကြည့်ပါ
ဒီ project မှာ variables, lists, dictionaries, functions, loops, condition, files, JSON, error handling အကုန်ပေါင်းသုံးထားတယ်။ Python basic ပြီးတဲ့အခါ project လေးတွေများများရေးတာ အရေးကြီးဆုံးပါ။