OK! Back to the task-listing code. In the spirit of making very, very small changes, let's wrap up the task-listing code in a function, then call the function:
tasks =  with open('task_list.txt') as f: for task_line in f.readlines(): tasks.append(task_line.strip()) def list_tasks(): for task in tasks: print(task) list_tasks()
If you don't know how Python functions work, now's a good time to learn. (The tutorial will wait for you!) Whether or not you already know what a function is, please take this step seriously. Wrapping code up into reusable units is one of the very most essential processes in software engineering. And we get it wrong all the time. It is fiendishly difficult to get right.
Please make sure that our single ad hoc test--running the script and verifying that the output is the same--still works.
Now wrap the task-retrieval code into its own function:
def get_all_tasks(): tasks =  with open('task_list.txt') as f: for task_line in f.readlines(): tasks.append(task_line.strip()) return tasks def list_tasks(tasks): for task in tasks: print(task) list_tasks(get_all_tasks())
This uses a few more of the things that Python functions can do. If it's confusing, that's OK! Go read about functions for a while. Again, it's not that I don't have anything to say--it's that others have a lot to say, and they're just a Google search away.
(The essential thing to see about the last line is that
get_all_tasks() is called, and its return value--a list with four elements--is passed to
list_tasks, a function that takes a single parameter. That four-element list is thus the value of tasks when
list_tasks runs, and each of its elements is in turn assigned to task and printed.)
Again, dividing code up into functions (and other units of code) is hard and very important to get right.
Next post: Python task manager from scratch, part 5: Setting up version control