Interactive Python To-Do App with Input and Loops Explained

2. Full Interactive To-Do App

tasks = []

def add_task():
    task = input("Enter a task: ")
    tasks.append(task)
    print("Task added!")

def show_tasks():
    if not tasks:
        print("No tasks yet.")
    else:
        print("Your Tasks:")
        for i, task in enumerate(tasks, start=1):
            print(str(i) + ".", task)

while True:
    print("\n--- To-Do Menu ---")
    print("1. Add Task")
    print("2. View Tasks")
    print("3. Exit")

    choice = input("Choose an option: ")

    if choice == "1":
        add_task()
    elif choice == "2":
        show_tasks()
    elif choice == "3":
        print("Goodbye!")
        break
    else:
        print("Invalid option. Try again.")

Explanation:

  • tasks = [] creates an empty list to store tasks.
  • add_task() gets input and adds it to the list.
  • show_tasks() loops through and prints each task.
  • while True creates an infinite loop until the user exits.
  • if-elif checks the user's menu choice.

Comments

Popular Posts