Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Python Create a Simple To-Do List

Python Create a Simple To-Do List

Document Object Reference Manual Python3 Examples

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

)

   

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 a task"

)

   

print

(

"2. Display the task list"

)

   

print

(

"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"

:

       

print

(

"Exiting the program."

)

       

break

   

else

:

       

print

(

"Invalid option, please try again."

)

Document Object Reference Manual Python3 Examples