Python Create a Simple To-Do List
A simple exercise is 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
)
(
f
"Task '{task}' added."
)
Define a function to display the task list
def
show_tasks
(
)
:
if
not
tasks:
(
"The task list is empty."
)
else
:
(
"Task list:"
)
for
index
,
task
in
enumerate
(
tasks
,
start
=
1
)
:
(
f
"{index}. {task}"
)
Main program loop
while
True
:
(
"\nChoose an option:"
)
(
"1. Add a task"
)
(
"2. Display the task list"
)
(
"3. Exit"
)
choice
=
input
(
"Enter the option number: "
)
if
choice
==
"1"
:
new_task
=
input
(
"Enter a new task: "
)
add_task
(
new_task
)
elif
choice
==
"2"
:
show_tasks
(
)
elif
choice
==
"3"
:
(
"Exiting the program."
)
break
else
:
(
"Invalid option, please try again."
)
Python3 Examples