Hey, in this video, we will work on the edit and the complete case.
So just to refresh your memory, let me show you how these two commands work currently.
So first let me just see the list of to dos and then enter edit as a command.
So as soon as we press edit.
The program asks us to enter A to do so.
That is the equivalent of this.
This line here produces that output.
Now, once we enter here a number, for example, if we want to edit number three, we enter three here.
So when we enter three, our program subtracts one from three because the list indexing starts from
zero.
So if we enter three as a user, actually we meant to say to the item with index two because this has
an index of zero, this is an index of one.
This has an index of two.
So play has an index of two.
Therefore, we get two here.
Then what we do is we get a new to do from the user.
So here we go and train you to do.
Play a game.
Press enter.
And once we entered play a game here.
So play a game is part of new to do.
And then we change in the list.
So we changed the item with index to to that new item.
So play will be replaced with play a game.
Now, if we exit the program, exit press enter and then we check to dos.
So if you want to refresh it, reload from disk.
No changes are written in the file.
Why?
Well, because.
We didn't write any files here.
We didn't handle any files.
So let's fix that in this video.
No you know that you can use the with context manager or.
But what exactly do you do here.
Do you read or do you edit the file?
Well, you do both.
First, you want to open the file in read mode.
So once you get the number from the user and you.
Subtract one from that number.
Then somewhere here, perhaps you want to get the existing list of to do items.
So in here I'll get that with context manager and just copy it here.
So that will give us the two do's in the list.
The existing to Jews in a list.
And then.
We get the new tattoos here, then we replace the new to do in the list.
So at this point, the list should have been modified.
I can prove that to you by checking with the print function.
To reduce.
Let me add some extra information here is to dos existing.
Existing to dos.
And then another function after we modify that to do list print.
Here is how it will be.
And again, we print out the tattoos variable and run the program.
Edit So we wanted to edit number three.
And so once I press three, which corresponds to this line here.
Then we get printed out this line and then we enter a new to do.
Play the game, press enter.
And here is the new list.
So play the game is not part of the list of bots.
You see that it's missing a backslash.
And so let's add a backslash in here.
So we want to replace the existing to do with new to do plus.
Beck slash rn.
And then we need to do something with that list.
So we need to write that list back to the file.
For that we need the right with context manager.
So I'm hoping that and putting it here at the end.
So just before the case complete line, we put that and perhaps we don't need these lines anymore,
so.
Remove that and this print function as well.
And that's about the ended case.
So I'll run this again.
So we have these edits.
Let's try number four.
So that is fix.
Fix it.
Well.
Show.
And there we go.
Fix it.
Well, is now number four exit check to make sure in the file.
Yeah, fix it.
Well, it's here, so it's working.
And let's also implement the complete case because this is also not connected to our file.
So we get the input from the user.
That's the index.
We remove that index from the list of boards.
We want to get the current list from the file again.
So again, we need this two lines.
So we get the current lists, we remove the item with that index from the list and then we write the
list back to the file using that code.
So paste it here and let's run.
So let's see what we have right then.
Say complete.
Which one do you want to complete?
Three Press enter show.
So play is not there anymore.
And let's exit and go to to Deuce.
And your play has disappeared.
If you want to make this complete comments more user friendly, perhaps you may want to show a message
to the user.
So I'll write a message f string construct and have string here and say to do.
So we want to tell the user that this to do was removed from the list.
So which is it to do?
Well, the removed to do, we can get it using list indexing so we get the number here, the index from
the user and then we get the lists.
And before we removing that to do from the list, we want to access it to store it in a variable.
Removed to do all you can say, to do to remove because removed do doesn't make so much sense here since
it has not been removed at this point from the list here it is removed actually.
So let's say to do to remove is equal to to dos.
Number minus one.
Or you could also say create a new variable here.
Index is equal to number minus one so that you don't have to perform this operation two times in the
code.
You just perform it once a year and then you replace this to that variable and also this to that variable.
So that's the beauty of variables and that will give us a to do to remove, which was play in our case.
And then we place that to do in here and that should do it.
So let's run this again complete or let's show first, then complete.
Let's try number to show her.
We didn't get any output because I forgot to print out the message.
So print.
Message.
Run again.
Show complete so that to do was completed, but it was not printed out.
Complete number two.
And so we get this to the fix it well was removed from the list.
Now you see this brake line here?
If you want to remove that brick line, you want to go back in here.
And say to dos that index dot strip and then goes backslash and so does this make sense to you?
Well, the idea is that this is a string, right?
So the situation here is like this.
We have a list to dos.
You know, item one, item two and so on.
So to DOS one, that's strip, what you're doing is actually applying the strip method to that string.
So that was just to be clear, you understand this and that's the final code.
So let me run it for one more time.
Show complete.
We want to complete number two this time and we get this message to do right was removed from the list
so that message is printed out correctly and that finishes this video.
Thanks a lot for following.
I'll see you.