WEBVTT

0
00:00.270 --> 00:04.410
So we've now got pretty much all the functionality that we want in order for

1
00:04.410 --> 00:06.930
this to function as a game.

2
00:07.620 --> 00:10.680
But if we want to be able to use this as a learning tool,

3
00:11.010 --> 00:15.510
then it would be great for us to be able to have some sort of way of typing an

4
00:15.540 --> 00:18.000
exit code. Say, if I typed exit,

5
00:18.030 --> 00:23.030
then I could exit out of the game and then I could get my code to generate a CSV

6
00:24.600 --> 00:29.600
file that contains all of the states that I missed from all 50 states.

7
00:30.600 --> 00:34.170
We know that in our while loop we can exit out of it

8
00:34.290 --> 00:37.440
as long as we use the break statement.

9
00:38.100 --> 00:40.920
What we can do is we can use an if statement to check

10
00:40.920 --> 00:45.270
well, if the answer_state was actually equal to the secret code

11
00:45.300 --> 00:49.860
which is exit, remember that the answer_state is going to be title cased

12
00:49.890 --> 00:54.540
so we have to check for it with a capital E, and if that happens,

13
00:54.780 --> 00:57.210
then we're gonna break out of our while loop

14
00:57.270 --> 01:00.060
which means it's going to end the while loop prematurely,

15
01:00.360 --> 01:04.560
and we can delete this line so that it actually ends the game

16
01:04.620 --> 01:08.730
and exits out of the window. So if we test this out,

17
01:09.480 --> 01:14.040
then you can see we can type our states or we can type exit

18
01:14.640 --> 01:16.290
and that will exit the game

19
01:16.410 --> 01:19.260
and we end up with process finished with exit code zero.

20
01:20.010 --> 01:23.310
This is the moment where we want to generate a new file

21
01:24.030 --> 01:28.410
and that file is going to be called states_to_learn.csv.

22
01:29.070 --> 01:31.800
And I want you to generate the CSV

23
01:31.830 --> 01:35.250
which is going to contain just the names of states

24
01:35.610 --> 01:40.610
which have not been guessed by the user when they exit the game.

25
01:41.820 --> 01:43.770
Have a think about how you would achieve this,

26
01:44.040 --> 01:45.750
pause the video and give that a go.

27
01:50.690 --> 01:53.300
All right. So let's think about this.

28
01:53.600 --> 01:58.600
What we want to do is to take the guessed states and compare it against all

29
01:59.840 --> 02:00.673
states,

30
02:01.100 --> 02:05.240
and then we want to create a new list of all the missing states.

31
02:05.720 --> 02:10.720
So let's create a new list called missing_states and then we can use a for loop

32
02:12.770 --> 02:17.270
to loop through each of the states inside all of the states.

33
02:17.780 --> 02:22.780
And if we find that a state is not in the guessed_states,

34
02:23.930 --> 02:25.820
well, then that means it's missing, right?

35
02:26.030 --> 02:30.590
So then we can tap into our missing states and append this state. And 

36
02:32.360 --> 02:33.260
by the end of this,

37
02:33.290 --> 02:37.430
we should end up with a list of all of the missing states.

38
02:38.060 --> 02:42.530
So let's test this code. So I'm just going to type in a few states,

39
02:42.680 --> 02:47.660
some of the ones beginning with A, so Alaska, um,

40
02:47.720 --> 02:48.830
Arizona,

41
02:49.280 --> 02:51.680
<v 1>Um, uh,</v>

42
02:51.740 --> 02:55.550
Arkansas,

43
02:56.780 --> 02:59.620
<v 0>and what's the last one, Alabama.</v>

44
03:02.350 --> 03:05.890
Okay. So I think that's all of the states beginning with A,

45
03:06.610 --> 03:11.610
so now I'm going to hit exit and it's going to give me a list of all the states

46
03:12.910 --> 03:16.240
that I'm missing. So if we take a look at all the A states,

47
03:16.330 --> 03:20.500
if we compare it against this list of 50 states, there's no Alabama,

48
03:20.500 --> 03:24.910
there's no Alaska, and there is no Arkansas because I guessed it correctly.

49
03:25.210 --> 03:28.660
So this is now just a list of all the ones I didn't manage to get.

50
03:29.650 --> 03:33.820
So now how can we turn this list into a CSV?

51
03:35.110 --> 03:35.440
Well,

52
03:35.440 --> 03:40.440
we can create a new data and that new data is going to be a data frame

53
03:42.250 --> 03:45.250
which is created from our missing_states.

54
03:45.700 --> 03:48.910
So this is just going to be a one column data frame.

55
03:50.410 --> 03:52.180
Once we've created that new data,

56
03:52.240 --> 03:55.990
then we can go ahead and save it as a CSV.

57
03:56.410 --> 04:01.270
And we're going to call that states_to_learn.csv,

58
04:01.900 --> 04:04.540
just as we've typed here. Now,

59
04:04.540 --> 04:07.990
when I run this code and I've typed

60
04:10.180 --> 04:13.600
all of the A states, and now I hit exit.

61
04:14.050 --> 04:16.840
Then you can see we've got a new file

62
04:16.840 --> 04:20.080
that's just been generated fresh out of the press

63
04:20.200 --> 04:25.200
and it is our states to learn with all 44 States that I'm missing from my game.

64
04:29.320 --> 04:31.870
I hope you had fun building this game with me,

65
04:32.080 --> 04:36.220
and I would love to know how many states you can name the first time you play

66
04:36.220 --> 04:39.760
this game. Cause obviously the more you play it, the more you get better.

67
04:39.910 --> 04:43.990
And I hope you'll use this as an educational tool. If you have students,

68
04:43.990 --> 04:46.330
if you are a geography teacher, if you have kids,

69
04:46.690 --> 04:50.500
you can get them to play with this awesome program that you've built.

70
04:50.920 --> 04:54.220
Remember what we've done here can be endlessly customized.

71
04:54.550 --> 04:56.740
You can make a version for your own country,

72
04:56.800 --> 05:00.640
or you can make one for the whole world where you have to name each country on

73
05:00.640 --> 05:03.880
the world map, or you can do some other sort of list,

74
05:04.060 --> 05:08.890
like name all of the plants on an image or name all of the animals.

75
05:09.100 --> 05:10.840
The world is really your oyster

76
05:10.900 --> 05:15.900
and I can't wait to see what you've created using your imagination and how

77
05:16.060 --> 05:19.060
you've customized the code. So once you've done that,

78
05:19.090 --> 05:24.090
be sure to post it in the Q/A of this lesson and so that we can all play and

79
05:24.430 --> 05:25.510
admire your code.