Welcome back.
Now let's move to the mini project to do list application.
Let's build a simple to do list application that lets the user add tasks.
Show all tasks.
Saves tasks to a file and uses functions, lists, files, and error handling.
We're going to review all the concepts in one to do list application.
At first let me start with loading task.
So here let me create a new function named as load task.
This function loads tasks from file.
Return empty list if file doesn't exist.
So here try with with open.
Let me save the file as tasks dot txt file.
And as I told you, we need to load the task.
So I'm going to load it in reading mode as file to read the saved tasks from the tasks dot.
TXT file.
When the programs start, we need to use this function load task functions and we need to surround it
with try and catch and true a try and accept it attempts to open this try it attempts to open the file
if the file doesn't exist.
For example, the first time we run the program, this operation will cause an error.
Except here we need to surround it with accept.
If any error occurs, like the file not existing, it simply catches the error and returns an empty
list.
This prevents the program from crashing.
Okay, it's very simple.
Now let's continue with this.
Try and start with tasks equals to file dot read lines.
This function reads all lines in the file and returns them as a list of strings.
Okay, each task is on its own line, so this is the function that reads all lines in the file and returns
them as a list of string.
So this is the file tasks dot txt and we are going to get all the tasks and read them one by one.
Remove the new line character.
In order to remove the new line character, we need to use This return statement.
So return task dot strip for task in all tasks you can, you can keep it.
Don't worry, you can keep it.
Also you can remove this line, but this is good for now in order not to not to to be complicated,
let me continue with the project.
Let me define a new function called save task and this function takes tasks as a parameter.
Try with open we need to open the file.
Make sure that it is similar to the name of the previously created task file, and this time we don't
need to read, we need to save.
So I'm going to use W as the permission okay as file.
Then for every task inside tasks, we need to loop through and save it to the file.
So file dot write task and the new line this thing.
This is the new line in Python back slash and n.
This is the new line.
Now let me use except print error saving tasks.
This is the save tasks function.
And this is the function for loading tasks okay.
This is for saving the tasks to the tasks dot txt and this is how to load the tasks dot txt file.
Now let me create another function named as add tasks or add task task task equals to input.
By the way, Python is very sensitive.
We need to remove this space in order to solve this error and make sure trie is aligned with except.
Def is aligned with def and others.
Okay, so input.
We need to display a message to the user.
Enter a task please.
Tasks dot append the task I'm using append to add it to that tasks similar to what we've done before.
Okay, so we are appending.
We are adding the newly created task to the end of the tasks array or list another function named as
view tasks, taking to the parameters tasks.
And here if tasks is true and it's not null, print your tasks and through go through all the tasks
this function displays all tasks in a numbered list format, or shows a message if there are no tasks.
Here we have the tasks parameter.
It expects a list of tasks to display then if the tasks are not empty.
If the tasks list is not empty in Python, empty lists are falsy and no empty lists are truthy.
Equivalent to writing, for example, if the length of tasks greater than zero.
So it's similar to this.
Writing tasks greater than zero.
It's similar to this.
So if the tasks are not empty, go and execute this.
Otherwise no tasks yet here.
Print your tasks only executes if there are tasks.
Prints a header for the task list.
Enumerate here.
Enumerate.
This is the key part that tasks and one enumerate loops through the list.
While keep tracking of the index, one means starting counting from one instead of the default zero.
For each iteration, it gives us I the number of one, two, three, four, five, six, and others,
and task the actual task in the tasks list.
And here we are using the F string to format the output between I and the task.
Let me run.
Everything is working fine.
Now we need to call those functions.
So in the next video we're going to continue.