We are now within sight of the finish line. We're onto the last step.
So this is the solution for step 4. And in step 4
we want to be able to improve our program so that the words where the user
clicks the checkmark to means they already know that word.
They know what it means and they don't wanna see it again in this list.
So what we want to be able to do is to remove the words that the user knows from
this dictionary of words to_learn. And to do that,
we have to change this command. When the user clicks on the unknown button,
we're going to simply just give them the next card,
which is a random word from the list of words.
But when they use it clicks on the known button,
instead of clicking on next card, we're going to call a different function
which I'm going to call is_known. Now is_known
is going to be a function
that's going to remove the current card from the cards that are in the list of
words to learn.
So what we're going to do is we're going to get hold of our list to learn,
and then we're going to call the remove method.
And then we're going to remove a particular element from it.
And that happens to be the current card.
So remember, the current card is a dictionary
which we got by randomly choosing from our list to learn.
So now, when the user says is_known,
then the current card is going to be removed from the list of words to learn.
And then after we've done that,
then we're going to call next card from this particular method. At the moment,
if we run this code and I just keep clicking the check mark,
eventually, we're going to have a very small list of words that we're going to
pick from. In fact,
if I go ahead and print the length of my list, to_learn,
you can see that the first time I run this code and I click on the is_known
button, we have a hundred words. But if I keep clicking on this,
then you'll see that every single time,
I'm just reducing that list because I'm saying,
I already know what this word compris means
and that word is now taken out of my list of words to learn.
So now that list is smaller by one entry.
Now this works right now, but if I rerun the app,
then you can see it goes back right to the beginning
and it has a hundred words again.
So in order to keep hold of the words that I still need to learn,
I have to save this list to a new permanent file
each time the user clicks on this is_known button.
So the way that I'm going to do that is by using pandas again.
I'm going to use pandas to create a new data frame.
And that data frame is going to be created from our list to_learn.
Now, I'm going to save this as our data,
and then I can say data.to_csv in order to save it as a CSV file.
So I'll call this words_to_learn.csv.
So now, if I press my check mark
and I take a look within my files,
you can see that words_to_learn.csv is created.
And at the moment it's got 89 entries. But if I keep clicking this a few times,
it's going to reduce each time.
So this is now a permanent storage of all the words that I have yet to learn.
But I don't really want it randomly in the middle of my project.
I want it to be saved alongside my french_words.csv.
So I have to change this to a file path
which is going to be data/words_to_learn.csv.
So let's go ahead and delete this file and do refactor.
And then we're going to run this again.
And next time you'll see that it appears in the right place.
Now, in addition to saving it to CSV,
we also have to read from that CSV because instead of using the words from our
original list which is our french_words.csv,
I actually want to be able to read from my words_to_learn.csv instead,
because that way, every single time I rerun the app,
it's always going to give me all the words that I've yet to learn.
So currently we're on 87. If I run this again,
you can see that the first time I click on this,
it's going to go to 86 instead of going back to 100.
But there is a problem here though
because if I delete this file, words_to_learn.csv, and I go ahead and run this, the first
time this runs it's going to crash and we have that familiar file
not found error. So we know how to deal with that.
We need to catch this exception. The exception occurs right here.
It happens when we try to get hold of this piece of data
which may or may not exist. So if it does not exist,
so we're going to catch this except file not found error. Well,
in that case, we're going to be using the original data
which comes from the french_words.csv. So we'll say pandas.
read_csv
and then we're going to read this file, data/french_words.csv.
So this is always going to be there because it's preloaded with our project.
But if we do manage to find our words_to_learn.csv,
then that means this program has been run before and we've removed some of the
words we already know. So we can catch that with an else statement and we can say,
well, in this case,
we're going to set a global variable called to_learn
which starts out as an empty dictionary.
And we set it to the data that comes from this CSV.
However, if that file was not found,
then this to_learn is going to be set to the original data.to_dict
and we're going to orient according to records as well.
1: Okay.
0: Now, when we run our code, you can see we have no errors.
And the first time that it runs,
it's going to pick from our french_words.csv. Now,
once I've started saying I've learned this, I've learned this,
I've learned this, then it's going to generate a words_to_learn.csv.
And the next time I run this code,
it's gonna start using that words_to_learn.csv and start where I left off.
Now, there is one slight weird bug to this.
When you take a look at the words_to_learn.csv,
it adds the record number to the first column
every single time I run the code. So if I run this code again,
you can see now I have three columns of all of the records.
So basically when we read from this particular CSV,
we generate a data frame. And when I read from that data frame,
so let's delete this and run our code again,
you can see that pandas is automatically added in these record numbers.
So that gets then added back into our words_to_learn.csv when it saves it into
the file. If we want to get rid of these record numbers,
all we have to do is when we save it to the CSV,
we can set a property called index to false. This way
it just doesn't add the index numbers to our newly created list
and now if I rerun this code,
you can see that our words_to_learn.csv
doesn't actually include an index. It only includes the actual records or
the actual words and their English translations.
That's
0: basically it. That's all the solutions to our capstone project.
And you've now built a fully fleshed flashcard that you can use to learn
languages.
So you could have the front with whatever language you want, and the back with
English translations, or you can use it to learn Programming facts.
You could have the front with a Programming word and the back as the meaning of
the Programming word.
2: You could use it to study History or Geography.
0: And this program is going to help you manage all the things that you don't
remember and keep showing it to you until you do. Have fun modifying this
flashcard app and I hope it will be helpful in your studies as well.
If you've built something particularly interesting, again,
be sure to share with us in the Q/A so that we can all admire and
congratulate you on your hard work.