Python Project: ToDo List

Project Description

The program you write can do 4 things.

  1. It can show the content of your ToDo list
  2. It can add an item to your ToDo list
  3. It can remove an item from your ToDo list
  4. It can quit the program

Step 1 The Data Model

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.

  1. It can show the content of your ToDo list – This can be done by iterating over the list and showing items.
  2. It can add an item to your ToDo list – This can be done by appending an item to the list.
  3. It can remove an item from your ToDo list – This can be done by removing an item from the list.
  4. It can quit the program – This is not relevant to the list.

You can consider other data structures or even implement your custom class.

Step 2 Program Structure

The overall structure of the program can be implemented in a while loop.

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.

Step 3 Implementation

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.

Learn Python

Learn Python A BEGINNERS GUIDE TO PYTHON

  • 70 pages to get you started on your journey to master Python.
  • How to install your setup with Anaconda.
  • Written description and introduction to all concepts.
  • Jupyter Notebooks prepared for 17 projects.

Python 101: A CRASH COURSE

  1. How to get started with this 8 hours Python 101: A CRASH COURSE.
  2. Best practices for learning Python.
  3. How to download the material to follow along and create projects.
  4. A chapter for each lesson with a descriptioncode snippets for easy reference, and links to a lesson video.

Expert Data Science Blueprint

Expert Data Science Blueprint

  • Master the Data Science Workflow for actionable data insights.
  • How to download the material to follow along and create projects.
  • A chapter to each lesson with a Description, Learning Objective, and link to the lesson video.

Machine Learning

Machine Learning – The Simple Path to Mastery

  • How to get started with Machine Learning.
  • How to download the material to follow along and make the projects.
  • One chapter for each lesson with a Description, Learning Objectives, and link to the lesson video.

Leave a Comment