Python Create a Simple To-Do List

Document 对象参考手册Python3 Examples

A simple exercise could be to create a simple to-do list program.

Example

# Simple to-do list program

# Create an empty task list
tasks = []

# Define a function to add a task
def add_task(task):
    tasks.append(task)
    print(f"Task '{task}' added.")

# Define a function to display the task list
def show_tasks():
    if not tasks:
        print("The task list is empty.")
    else:
        print("Task list:")
        for index, task in enumerate(tasks, start=1):
            print(f"{index}. {task}")

# Main program loop
while True:
    print("\nChoose an option:)
    print(1. Add task)
    print(2. Show task list)
    print(3. Exit)

    choice = input(Enter option number:)

    if choice == "1":
        new_task = input(Enter a new task:)
        add_task(new_task)
    elif choice == "2":
        show_tasks()
    elif choice == "3":
        print(Exiting program.)
        break
    else:
        print(Invalid option, please re-enter.)

Document 对象参考手册Python3 Examples

Other Extensions