1
00:00:00,300 --> 00:00:04,680
We are now within sight of the finish line. We're onto the last step.

2
00:00:04,710 --> 00:00:09,240
So this is the solution for step 4. And in step 4

3
00:00:09,270 --> 00:00:14,270
we want to be able to improve our program so that the words where the user

4
00:00:14,910 --> 00:00:18,780
clicks the checkmark to means they already know that word.

5
00:00:18,810 --> 00:00:23,040
They know what it means and they don't wanna see it again in this list.

6
00:00:23,580 --> 00:00:28,580
So what we want to be able to do is to remove the words that the user knows from

7
00:00:29,010 --> 00:00:33,510
this dictionary of words to_learn. And to do that,

8
00:00:33,630 --> 00:00:38,610
we have to change this command. When the user clicks on the unknown button,

9
00:00:38,880 --> 00:00:41,340
we're going to simply just give them the next card,

10
00:00:41,370 --> 00:00:44,700
which is a random word from the list of words.

11
00:00:45,210 --> 00:00:47,850
But when they use it clicks on the known button,

12
00:00:48,000 --> 00:00:52,440
instead of clicking on next card, we're going to call a different function

13
00:00:52,710 --> 00:00:56,610
which I'm going to call is_known. Now is_known

14
00:00:56,640 --> 00:00:58,050
is going to be a function

15
00:00:58,110 --> 00:01:03,110
that's going to remove the current card from the cards that are in the list of

16
00:01:06,300 --> 00:01:07,170
words to learn.

17
00:01:07,830 --> 00:01:12,120
So what we're going to do is we're going to get hold of our list to learn,

18
00:01:13,470 --> 00:01:17,070
and then we're going to call the remove method.

19
00:01:17,610 --> 00:01:20,460
And then we're going to remove a particular element from it.

20
00:01:20,880 --> 00:01:24,240
And that happens to be the current card.

21
00:01:25,410 --> 00:01:28,770
So remember, the current card is a dictionary

22
00:01:29,040 --> 00:01:33,480
which we got by randomly choosing from our list to learn.

23
00:01:34,080 --> 00:01:36,990
So now, when the user says is_known,

24
00:01:37,320 --> 00:01:41,670
then the current card is going to be removed from the list of words to learn.

25
00:01:42,120 --> 00:01:43,560
And then after we've done that,

26
00:01:43,920 --> 00:01:48,920
then we're going to call next card from this particular method. At the moment,

27
00:01:49,440 --> 00:01:53,250
if we run this code and I just keep clicking the check mark,

28
00:01:53,670 --> 00:01:58,670
eventually, we're going to have a very small list of words that we're going to

29
00:01:59,370 --> 00:02:01,260
pick from. In fact,

30
00:02:01,290 --> 00:02:06,290
if I go ahead and print the length of my list, to_learn,

31
00:02:08,190 --> 00:02:13,190
you can see that the first time I run this code and I click on the is_known

32
00:02:14,640 --> 00:02:19,020
button, we have a hundred words. But if I keep clicking on this,

33
00:02:19,050 --> 00:02:21,090
then you'll see that every single time,

34
00:02:21,090 --> 00:02:25,050
I'm just reducing that list because I'm saying,

35
00:02:25,080 --> 00:02:27,660
I already know what this word compris means

36
00:02:28,110 --> 00:02:31,950
and that word is now taken out of my list of words to learn.

37
00:02:32,250 --> 00:02:36,630
So now that list is smaller by one entry.

38
00:02:37,530 --> 00:02:41,670
Now this works right now, but if I rerun the app,

39
00:02:41,700 --> 00:02:45,540
then you can see it goes back right to the beginning

40
00:02:45,750 --> 00:02:47,550
and it has a hundred words again.

41
00:02:48,150 --> 00:02:52,950
So in order to keep hold of the words that I still need to learn,

42
00:02:53,280 --> 00:02:57,540
I have to save this list to a new permanent file

43
00:02:57,840 --> 00:03:01,000
each time the user clicks on this is_known button.

44
00:03:01,780 --> 00:03:06,220
So the way that I'm going to do that is by using pandas again.

45
00:03:06,790 --> 00:03:10,900
I'm going to use pandas to create a new data frame.

46
00:03:11,530 --> 00:03:15,970
And that data frame is going to be created from our list to_learn.

47
00:03:17,230 --> 00:03:19,540
Now, I'm going to save this as our data,

48
00:03:20,170 --> 00:03:25,170
and then I can say data.to_csv in order to save it as a CSV file.

49
00:03:26,350 --> 00:03:31,350
So I'll call this words_to_learn.csv.

50
00:03:32,860 --> 00:03:37,860
So now, if I press my check mark

51
00:03:38,980 --> 00:03:41,740
and I take a look within my files,

52
00:03:41,770 --> 00:03:44,980
you can see that words_to_learn.csv is created.

53
00:03:45,490 --> 00:03:50,380
And at the moment it's got 89 entries. But if I keep clicking this a few times,

54
00:03:50,620 --> 00:03:52,570
it's going to reduce each time.

55
00:03:52,960 --> 00:03:57,250
So this is now a permanent storage of all the words that I have yet to learn.

56
00:03:57,940 --> 00:04:01,510
But I don't really want it randomly in the middle of my project.

57
00:04:01,750 --> 00:04:05,170
I want it to be saved alongside my french_words.csv.

58
00:04:05,740 --> 00:04:08,200
So I have to change this to a file path

59
00:04:08,230 --> 00:04:11,110
which is going to be data/words_to_learn.csv.

60
00:04:11,620 --> 00:04:16,620
So let's go ahead and delete this file and do refactor.

61
00:04:17,110 --> 00:04:18,760
And then we're going to run this again.

62
00:04:18,880 --> 00:04:22,630
And next time you'll see that it appears in the right place.

63
00:04:23,530 --> 00:04:26,590
Now, in addition to saving it to CSV,

64
00:04:26,620 --> 00:04:31,620
we also have to read from that CSV because instead of using the words from our

65
00:04:32,680 --> 00:04:35,860
original list which is our french_words.csv,

66
00:04:36,400 --> 00:04:41,400
I actually want to be able to read from my words_to_learn.csv instead,

67
00:04:42,100 --> 00:04:45,460
because that way, every single time I rerun the app,

68
00:04:45,760 --> 00:04:49,570
it's always going to give me all the words that I've yet to learn.

69
00:04:50,020 --> 00:04:53,320
So currently we're on 87. If I run this again,

70
00:04:53,560 --> 00:04:55,840
you can see that the first time I click on this,

71
00:04:55,930 --> 00:04:59,560
it's going to go to 86 instead of going back to 100.

72
00:05:00,220 --> 00:05:01,750
But there is a problem here though

73
00:05:02,080 --> 00:05:07,080
because if I delete this file, words_to_learn.csv, and I go ahead and run this, the first

74
00:05:09,250 --> 00:05:13,540
time this runs it's going to crash and we have that familiar file

75
00:05:13,540 --> 00:05:16,810
not found error. So we know how to deal with that.

76
00:05:16,840 --> 00:05:21,280
We need to catch this exception. The exception occurs right here.

77
00:05:21,940 --> 00:05:26,940
It happens when we try to get hold of this piece of data

78
00:05:27,640 --> 00:05:32,290
which may or may not exist. So if it does not exist,

79
00:05:32,470 --> 00:05:36,820
so we're going to catch this except file not found error. Well,

80
00:05:36,820 --> 00:05:40,390
in that case, we're going to be using the original data

81
00:05:41,050 --> 00:05:45,970
which comes from the french_words.csv. So we'll say pandas.

82
00:05:46,000 --> 00:05:47,050
read_csv

83
00:05:47,140 --> 00:05:52,090
and then we're going to read this file, data/french_words.csv.

84
00:05:52,660 --> 00:05:56,890
So this is always going to be there because it's preloaded with our project.

85
00:05:57,500 --> 00:06:01,070
But if we do manage to find our words_to_learn.csv,

86
00:06:01,130 --> 00:06:05,810
then that means this program has been run before and we've removed some of the

87
00:06:05,810 --> 00:06:10,610
words we already know. So we can catch that with an else statement and we can say,

88
00:06:10,610 --> 00:06:11,900
well, in this case,

89
00:06:12,260 --> 00:06:16,130
we're going to set a global variable called to_learn

90
00:06:16,310 --> 00:06:18,770
which starts out as an empty dictionary.

91
00:06:19,250 --> 00:06:23,780
And we set it to the data that comes from this CSV.

92
00:06:24,620 --> 00:06:27,620
However, if that file was not found,

93
00:06:27,950 --> 00:06:32,950
then this to_learn is going to be set to the original data.to_dict

94
00:06:34,790 --> 00:06:39,410
and we're going to orient according to records as well.

95
00:06:43,510 --> 00:06:43,660
Okay.

96
00:06:43,660 --> 00:06:47,590
Now, when we run our code, you can see we have no errors.

97
00:06:48,010 --> 00:06:49,870
And the first time that it runs,

98
00:06:49,900 --> 00:06:54,310
it's going to pick from our french_words.csv. Now,

99
00:06:54,310 --> 00:06:58,480
once I've started saying I've learned this, I've learned this,

100
00:06:58,480 --> 00:07:03,130
I've learned this, then it's going to generate a words_to_learn.csv.

101
00:07:03,580 --> 00:07:05,560
And the next time I run this code,

102
00:07:05,860 --> 00:07:10,860
it's gonna start using that words_to_learn.csv and start where I left off.

103
00:07:11,800 --> 00:07:15,430
Now, there is one slight weird bug to this.

104
00:07:15,820 --> 00:07:19,060
When you take a look at the words_to_learn.csv,

105
00:07:19,750 --> 00:07:23,590
it adds the record number to the first column

106
00:07:23,950 --> 00:07:27,850
every single time I run the code. So if I run this code again,

107
00:07:28,210 --> 00:07:31,960
you can see now I have three columns of all of the records.

108
00:07:32,500 --> 00:07:37,330
So basically when we read from this particular CSV,

109
00:07:37,780 --> 00:07:42,580
we generate a data frame. And when I read from that data frame,

110
00:07:42,940 --> 00:07:47,800
so let's delete this and run our code again,

111
00:07:49,090 --> 00:07:53,860
you can see that pandas is automatically added in these record numbers.

112
00:07:54,280 --> 00:07:59,280
So that gets then added back into our words_to_learn.csv when it saves it into

113
00:08:02,860 --> 00:08:06,790
the file. If we want to get rid of these record numbers,

114
00:08:07,000 --> 00:08:10,330
all we have to do is when we save it to the CSV,

115
00:08:10,870 --> 00:08:15,870
we can set a property called index to false. This way

116
00:08:16,150 --> 00:08:20,800
it just doesn't add the index numbers to our newly created list

117
00:08:21,160 --> 00:08:22,990
and now if I rerun this code,

118
00:08:23,440 --> 00:08:26,260
you can see that our words_to_learn.csv

119
00:08:26,560 --> 00:08:31,560
doesn't actually include an index. It only includes the actual records or

120
00:08:31,660 --> 00:08:34,480
the actual words and their English translations.

121
00:08:36,700 --> 00:08:37,059
That's

122
00:08:37,059 --> 00:08:41,770
basically it. That's all the solutions to our capstone project.

123
00:08:42,309 --> 00:08:47,140
And you've now built a fully fleshed flashcard that you can use to learn

124
00:08:47,140 --> 00:08:48,160
languages.

125
00:08:48,190 --> 00:08:52,370
So you could have the front with whatever language you want, and the back with

126
00:08:52,440 --> 00:08:56,790
English translations, or you can use it to learn Programming facts.

127
00:08:56,790 --> 00:09:01,500
You could have the front with a Programming word and the back as the meaning of

128
00:09:01,500 --> 00:09:02,520
the Programming word.

129
00:09:02,780 --> 00:09:05,540
You could use it to study History or Geography.

130
00:09:06,050 --> 00:09:10,520
And this program is going to help you manage all the things that you don't

131
00:09:10,520 --> 00:09:15,520
remember and keep showing it to you until you do. Have fun modifying this

132
00:09:16,520 --> 00:09:20,300
flashcard app and I hope it will be helpful in your studies as well.

133
00:09:21,170 --> 00:09:24,080
If you've built something particularly interesting, again,

134
00:09:24,140 --> 00:09:28,370
be sure to share with us in the Q/A so that we can all admire and

135
00:09:28,370 --> 00:09:30,350
congratulate you on your hard work.

