Putting it together
- Python course
- Beginner
- Lesson 87/116
- 6 min
Write a CLI app with a menu loop.
Putting the Todo CLI together
All pieces are ready: the tasks list, the add_task, list_tasks and complete_task functions. Now we put them together:
add_task("Buy bread")
add_task("Buy milk")
list_tasks()
complete_task(0)
list_tasks()This call order shows a typical usage flow. First tasks are added, then listed, one is completed and listed again.
The full flow
A full flow runs like this:
add_task("Buy bread")
add_task("Buy milk")
list_tasks()
complete_task(0)
list_tasks()The output: first the empty list, then tasks 0 and 1 are listed, then task 0 is updated to "Buy bread [done]" and the list is printed again.
This project is the skeleton of real CLI apps; in real life it can also be used with command-line arguments.
Practice Call the full flow
Add the calls add_task("Buy bread"), add_task("Buy milk"), list_tasks(), complete_task(0) and list_tasks() in order.
Starter code
# Todo CLI - bos gorev listesi
tasks = []
print(tasks)
def add_task(title):
tasks.append(title)
def list_tasks():
for i, t in enumerate(tasks):
print(i, t)
def complete_task(index):
if 0 <= index < len(tasks):
tasks[index] = tasks[index] + " [tamam]"
# Tum akisi cagirSample solution
add_task("Buy bread")
add_task("Buy milk")
list_tasks()
complete_task(0)
list_tasks()Expected output
[] 0 Buy bread 1 Buy milk 0 Buy bread [done] 1 Buy milk
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 Which call order is logical?
- add_task, list_tasks, complete_task Correct answer
- list_tasks, add_task, complete_task
- add_task, complete_task, list_tasks
- complete_task, list_tasks, add_task
Explanation: First add tasks, then list, then complete.
2 Complete the call
add_task("Buy bread")
add_task("Buy milk")
____()
complete_task(0)
list_tasks()
add_task("Buy bread")
add_task("Buy milk")
list_tasks()
complete_task(0)
list_tasks()
Explanation: list_tasks() lists the tasks.
3 Match the project pieces
| Term | Meaning |
|---|---|
tasks = [] |
Data model |
add_task |
Add |
list_tasks |
List |
complete_task |
Complete |