Implement a ToDo list in Python and you will learn a few things, including.
Are you ready for this?
The ToDo list Python program you will write can do 4 things.
Deciding how the data is represented is the most crucial decision. Most do not think that there are many options.
This choice is not given in the project description but impacts most of the other code. To see if a data model is a good choice, you should consider how it affects the implementation of the steps.
Here we will consider the Python list as our first choice.
To see if this is a good choice, we can see how it impacts the program features.
You can consider other data structures or even implement your custom class.
The overall structure of the program can be implemented in a while loop with conditionals. While the data structure we use is Python list.
A great way to have it is in an infinite while loop, which will only be terminated if the user chooses the quit. option.
Let’s consider this code.
todo = []
while True:
# print the options to the user
print('1: Show content')
print('2: Add an item to list')
print('3: Remove an item from the list')
print('4: Quit')
# prompt the user
option = input('Choose option: ')
if option == '1':
pass
elif option == '2':
pass
elif option == '3':
pass
elif option == '4':
print('Goodbye')
break
else:
print('Not understood')
The quit option is implemented with a break in the infinite while loop (while True).
Now we just need to implement the rest of the code.
The full code is implemented here.
todo = []
while True:
# print the options to the user
print('1: Show content')
print('2: Add an item to list')
print('3: Remove an item from the list')
print('4: Quit')
# prompt the user
option = input('Choose option: ')
if option == '1':
for idx, item in enumerate(todo):
print(idx, item)
elif option == '2':
item = input('Add item to list: ')
todo.append(item)
elif option == '3':
idx_str = input('What item to remove (use index): ')
idx = int(idx_str)
todo.pop(idx)
elif option == '4':
print('Goodbye')
break
else:
print('Not understood')
We use an enumerate under option 1 to get the index of each item in the list. This makes our implementation easier, as you can use this index when you delete an item under option 2.
This is part of 19 Python Projects and you can find another one and create the classical ELIZA in 13 lines of code.
Build and Deploy an AI App with Python Flask, OpenAI API, and Google Cloud: In…
Python REST APIs with gcloud Serverless In the fast-paced world of application development, building robust…
App Development with Python using Docker Are you an aspiring app developer looking to level…
Why Value-driven Data Science is the Key to Your Success In the world of data…
Harnessing the Power of Project-Based Learning and Python for Machine Learning Mastery In today's data-driven…
Is Python the right choice for Machine Learning? Should you learn Python for Machine Learning?…