1
00:00:00,270 --> 00:00:04,410
So we've now got pretty much all the functionality that we want in order for

2
00:00:04,410 --> 00:00:06,930
this to function as a game.

3
00:00:07,620 --> 00:00:10,680
But if we want to be able to use this as a learning tool,

4
00:00:11,010 --> 00:00:15,510
then it would be great for us to be able to have some sort of way of typing an

5
00:00:15,540 --> 00:00:18,000
exit code. Say, if I typed exit,

6
00:00:18,030 --> 00:00:23,030
then I could exit out of the game and then I could get my code to generate a CSV

7
00:00:24,600 --> 00:00:29,600
file that contains all of the states that I missed from all 50 states.

8
00:00:30,600 --> 00:00:34,170
We know that in our while loop we can exit out of it

9
00:00:34,290 --> 00:00:37,440
as long as we use the break statement.

10
00:00:38,100 --> 00:00:40,920
What we can do is we can use an if statement to check

11
00:00:40,920 --> 00:00:45,270
well, if the answer_state was actually equal to the secret code

12
00:00:45,300 --> 00:00:49,860
which is exit, remember that the answer_state is going to be title cased

13
00:00:49,890 --> 00:00:54,540
so we have to check for it with a capital E, and if that happens,

14
00:00:54,780 --> 00:00:57,210
then we're gonna break out of our while loop

15
00:00:57,270 --> 00:01:00,060
which means it's going to end the while loop prematurely,

16
00:01:00,360 --> 00:01:04,560
and we can delete this line so that it actually ends the game

17
00:01:04,620 --> 00:01:08,730
and exits out of the window. So if we test this out,

18
00:01:09,480 --> 00:01:14,040
then you can see we can type our states or we can type exit

19
00:01:14,640 --> 00:01:16,290
and that will exit the game

20
00:01:16,410 --> 00:01:19,260
and we end up with process finished with exit code zero.

21
00:01:20,010 --> 00:01:23,310
This is the moment where we want to generate a new file

22
00:01:24,030 --> 00:01:28,410
and that file is going to be called states_to_learn.csv.

23
00:01:29,070 --> 00:01:31,800
And I want you to generate the CSV

24
00:01:31,830 --> 00:01:35,250
which is going to contain just the names of states

25
00:01:35,610 --> 00:01:40,610
which have not been guessed by the user when they exit the game.

26
00:01:41,820 --> 00:01:43,770
Have a think about how you would achieve this,

27
00:01:44,040 --> 00:01:45,750
pause the video and give that a go.

28
00:01:50,690 --> 00:01:53,300
All right. So let's think about this.

29
00:01:53,600 --> 00:01:58,600
What we want to do is to take the guessed states and compare it against all

30
00:01:59,840 --> 00:02:00,673
states,

31
00:02:01,100 --> 00:02:05,240
and then we want to create a new list of all the missing states.

32
00:02:05,720 --> 00:02:10,720
So let's create a new list called missing_states and then we can use a for loop

33
00:02:12,770 --> 00:02:17,270
to loop through each of the states inside all of the states.

34
00:02:17,780 --> 00:02:22,780
And if we find that a state is not in the guessed_states,

35
00:02:23,930 --> 00:02:25,820
well, then that means it's missing, right?

36
00:02:26,030 --> 00:02:30,590
So then we can tap into our missing states and append this state. And 

37
00:02:32,360 --> 00:02:33,260
by the end of this,

38
00:02:33,290 --> 00:02:37,430
we should end up with a list of all of the missing states.

39
00:02:38,060 --> 00:02:42,530
So let's test this code. So I'm just going to type in a few states,

40
00:02:42,680 --> 00:02:47,660
some of the ones beginning with A, so Alaska, um,

41
00:02:47,720 --> 00:02:48,830
Arizona,

42
00:02:49,280 --> 00:02:51,680
Um, uh,

43
00:02:51,740 --> 00:02:55,550
Arkansas,

44
00:02:56,780 --> 00:02:59,620
and what's the last one, Alabama.

45
00:03:02,350 --> 00:03:05,890
Okay. So I think that's all of the states beginning with A,

46
00:03:06,610 --> 00:03:11,610
so now I'm going to hit exit and it's going to give me a list of all the states

47
00:03:12,910 --> 00:03:16,240
that I'm missing. So if we take a look at all the A states,

48
00:03:16,330 --> 00:03:20,500
if we compare it against this list of 50 states, there's no Alabama,

49
00:03:20,500 --> 00:03:24,910
there's no Alaska, and there is no Arkansas because I guessed it correctly.

50
00:03:25,210 --> 00:03:28,660
So this is now just a list of all the ones I didn't manage to get.

51
00:03:29,650 --> 00:03:33,820
So now how can we turn this list into a CSV?

52
00:03:35,110 --> 00:03:35,440
Well,

53
00:03:35,440 --> 00:03:40,440
we can create a new data and that new data is going to be a data frame

54
00:03:42,250 --> 00:03:45,250
which is created from our missing_states.

55
00:03:45,700 --> 00:03:48,910
So this is just going to be a one column data frame.

56
00:03:50,410 --> 00:03:52,180
Once we've created that new data,

57
00:03:52,240 --> 00:03:55,990
then we can go ahead and save it as a CSV.

58
00:03:56,410 --> 00:04:01,270
And we're going to call that states_to_learn.csv,

59
00:04:01,900 --> 00:04:04,540
just as we've typed here. Now,

60
00:04:04,540 --> 00:04:07,990
when I run this code and I've typed

61
00:04:10,180 --> 00:04:13,600
all of the A states, and now I hit exit.

62
00:04:14,050 --> 00:04:16,839
Then you can see we've got a new file

63
00:04:16,839 --> 00:04:20,079
that's just been generated fresh out of the press

64
00:04:20,200 --> 00:04:25,200
and it is our states to learn with all 44 States that I'm missing from my game.

65
00:04:29,320 --> 00:04:31,870
I hope you had fun building this game with me,

66
00:04:32,080 --> 00:04:36,220
and I would love to know how many states you can name the first time you play

67
00:04:36,220 --> 00:04:39,760
this game. Cause obviously the more you play it, the more you get better.

68
00:04:39,910 --> 00:04:43,990
And I hope you'll use this as an educational tool. If you have students,

69
00:04:43,990 --> 00:04:46,330
if you are a geography teacher, if you have kids,

70
00:04:46,690 --> 00:04:50,500
you can get them to play with this awesome program that you've built.

71
00:04:50,920 --> 00:04:54,220
Remember what we've done here can be endlessly customized.

72
00:04:54,550 --> 00:04:56,740
You can make a version for your own country,

73
00:04:56,800 --> 00:05:00,640
or you can make one for the whole world where you have to name each country on

74
00:05:00,640 --> 00:05:03,880
the world map, or you can do some other sort of list,

75
00:05:04,060 --> 00:05:08,890
like name all of the plants on an image or name all of the animals.

76
00:05:09,100 --> 00:05:10,840
The world is really your oyster

77
00:05:10,900 --> 00:05:15,900
and I can't wait to see what you've created using your imagination and how

78
00:05:16,060 --> 00:05:19,060
you've customized the code. So once you've done that,

79
00:05:19,090 --> 00:05:24,090
be sure to post it in the Q/A of this lesson and so that we can all play and

80
00:05:24,430 --> 00:05:25,510
admire your code.

