WEBVTT

00:00.080 --> 00:00.960
Welcome back.

00:01.200 --> 00:06.400
Now let's move to the mini project to do list application.

00:06.520 --> 00:13.040
Let's build a simple to do list application that lets the user add tasks.

00:13.200 --> 00:14.960
Show all tasks.

00:15.120 --> 00:23.120
Saves tasks to a file and uses functions, lists, files, and error handling.

00:23.160 --> 00:29.080
We're going to review all the concepts in one to do list application.

00:29.240 --> 00:33.440
At first let me start with loading task.

00:33.680 --> 00:39.840
So here let me create a new function named as load task.

00:40.000 --> 00:43.840
This function loads tasks from file.

00:43.880 --> 00:47.440
Return empty list if file doesn't exist.

00:47.520 --> 00:51.640
So here try with with open.

00:51.760 --> 00:57.220
Let me save the file as tasks dot txt file.

00:57.220 --> 01:02.300
And as I told you, we need to load the task.

01:02.500 --> 01:11.420
So I'm going to load it in reading mode as file to read the saved tasks from the tasks dot.

01:11.420 --> 01:12.660
TXT file.

01:12.660 --> 01:22.020
When the programs start, we need to use this function load task functions and we need to surround it

01:22.020 --> 01:31.500
with try and catch and true a try and accept it attempts to open this try it attempts to open the file

01:31.660 --> 01:33.820
if the file doesn't exist.

01:33.820 --> 01:39.140
For example, the first time we run the program, this operation will cause an error.

01:39.300 --> 01:43.100
Except here we need to surround it with accept.

01:43.220 --> 01:52.100
If any error occurs, like the file not existing, it simply catches the error and returns an empty

01:52.320 --> 01:53.120
list.

01:53.360 --> 01:56.280
This prevents the program from crashing.

01:56.600 --> 01:59.000
Okay, it's very simple.

01:59.040 --> 02:01.360
Now let's continue with this.

02:01.360 --> 02:08.560
Try and start with tasks equals to file dot read lines.

02:08.680 --> 02:17.120
This function reads all lines in the file and returns them as a list of strings.

02:17.160 --> 02:27.960
Okay, each task is on its own line, so this is the function that reads all lines in the file and returns

02:27.960 --> 02:30.160
them as a list of string.

02:30.520 --> 02:40.040
So this is the file tasks dot txt and we are going to get all the tasks and read them one by one.

02:40.160 --> 02:42.960
Remove the new line character.

02:43.080 --> 02:50.660
In order to remove the new line character, we need to use This return statement.

02:50.660 --> 02:59.660
So return task dot strip for task in all tasks you can, you can keep it.

02:59.660 --> 03:01.860
Don't worry, you can keep it.

03:01.900 --> 03:10.060
Also you can remove this line, but this is good for now in order not to not to to be complicated,

03:10.060 --> 03:13.540
let me continue with the project.

03:13.540 --> 03:22.340
Let me define a new function called save task and this function takes tasks as a parameter.

03:22.380 --> 03:26.380
Try with open we need to open the file.

03:26.660 --> 03:34.900
Make sure that it is similar to the name of the previously created task file, and this time we don't

03:34.900 --> 03:37.140
need to read, we need to save.

03:37.140 --> 03:43.060
So I'm going to use W as the permission okay as file.

03:43.300 --> 03:51.840
Then for every task inside tasks, we need to loop through and save it to the file.

03:51.840 --> 03:57.280
So file dot write task and the new line this thing.

03:57.320 --> 04:02.520
This is the new line in Python back slash and n.

04:02.560 --> 04:03.600
This is the new line.

04:03.640 --> 04:10.280
Now let me use except print error saving tasks.

04:10.280 --> 04:12.680
This is the save tasks function.

04:12.680 --> 04:17.480
And this is the function for loading tasks okay.

04:17.680 --> 04:25.960
This is for saving the tasks to the tasks dot txt and this is how to load the tasks dot txt file.

04:26.000 --> 04:36.640
Now let me create another function named as add tasks or add task task task equals to input.

04:36.640 --> 04:39.520
By the way, Python is very sensitive.

04:39.520 --> 04:47.690
We need to remove this space in order to solve this error and make sure trie is aligned with except.

04:47.850 --> 04:50.810
Def is aligned with def and others.

04:50.850 --> 04:52.770
Okay, so input.

04:52.810 --> 04:55.650
We need to display a message to the user.

04:55.650 --> 04:57.810
Enter a task please.

04:58.010 --> 05:06.530
Tasks dot append the task I'm using append to add it to that tasks similar to what we've done before.

05:06.650 --> 05:09.330
Okay, so we are appending.

05:09.330 --> 05:18.690
We are adding the newly created task to the end of the tasks array or list another function named as

05:18.730 --> 05:23.210
view tasks, taking to the parameters tasks.

05:23.210 --> 05:34.770
And here if tasks is true and it's not null, print your tasks and through go through all the tasks

05:34.930 --> 05:43.430
this function displays all tasks in a numbered list format, or shows a message if there are no tasks.

05:43.550 --> 05:46.310
Here we have the tasks parameter.

05:46.350 --> 05:54.070
It expects a list of tasks to display then if the tasks are not empty.

05:54.270 --> 06:03.910
If the tasks list is not empty in Python, empty lists are falsy and no empty lists are truthy.

06:04.030 --> 06:10.110
Equivalent to writing, for example, if the length of tasks greater than zero.

06:10.230 --> 06:12.630
So it's similar to this.

06:12.870 --> 06:16.750
Writing tasks greater than zero.

06:16.910 --> 06:19.310
It's similar to this.

06:19.470 --> 06:25.430
So if the tasks are not empty, go and execute this.

06:25.430 --> 06:29.270
Otherwise no tasks yet here.

06:29.310 --> 06:34.810
Print your tasks only executes if there are tasks.

06:34.850 --> 06:38.010
Prints a header for the task list.

06:38.010 --> 06:39.970
Enumerate here.

06:40.250 --> 06:41.330
Enumerate.

06:41.370 --> 06:49.290
This is the key part that tasks and one enumerate loops through the list.

06:49.290 --> 06:58.570
While keep tracking of the index, one means starting counting from one instead of the default zero.

06:58.610 --> 07:06.690
For each iteration, it gives us I the number of one, two, three, four, five, six, and others,

07:06.850 --> 07:12.050
and task the actual task in the tasks list.

07:12.050 --> 07:19.490
And here we are using the F string to format the output between I and the task.

07:19.770 --> 07:20.810
Let me run.

07:20.850 --> 07:22.730
Everything is working fine.

07:22.770 --> 07:25.930
Now we need to call those functions.

07:25.930 --> 07:28.130
So in the next video we're going to continue.
