Skip to main content

Add The Menu

Now, we will add the menu to the app. The menu is a loop that will run until the user quits the app. The menu will ask the user to enter a command, until the user enters q to quit the app. To get the user command, we are using the builtin input function. It will return a string containing the user input.

In the below code, we add two menu options: 1 to add a todo, and q to quit the app.

# main.py

def main():
todos = []

while True:
print_menu()
command = input("Please enter a command: ")
if command == "1":
todo = input("Enter a todo: ")
todos = add_todo(todos, todo)
elif command == "q":
break

def print_menu():
print("Press 1 to add todo")
print("Press 2 to print todos")
print("Press 3 to remove todo")
print("Press q to quit")

def add_todo(todos: list, todo: str):
todos.append(todo)
return todos

def print_todos(todos: list):
for todo in todos:
print(f"- {todo}")


def remove_todo(todos: list, index: int):
todos.pop(index)
return todos

# this will run the `main` function when we run the program `python3 main.py`
if __name__ == "__main__":
main()

Assignment

Now, your assignment is to add the following menu options:

  • 2 to print the todos
  • 3 to remove a todo
  • 4 to mark a todo as done