So we've now got pretty much all the functionality that we want in order for
this to function as a game.
But if we want to be able to use this as a learning tool,
then it would be great for us to be able to have some sort of way of typing an
exit code. Say, if I typed exit,
then I could exit out of the game and then I could get my code to generate a CSV
file that contains all of the states that I missed from all 50 states.
We know that in our while loop we can exit out of it
as long as we use the break statement.
What we can do is we can use an if statement to check
well, if the answer_state was actually equal to the secret code
which is exit, remember that the answer_state is going to be title cased
so we have to check for it with a capital E, and if that happens,
then we're gonna break out of our while loop
which means it's going to end the while loop prematurely,
and we can delete this line so that it actually ends the game
and exits out of the window. So if we test this out,
then you can see we can type our states or we can type exit
and that will exit the game
and we end up with process finished with exit code zero.
This is the moment where we want to generate a new file
and that file is going to be called states_to_learn.csv.
And I want you to generate the CSV
which is going to contain just the names of states
which have not been guessed by the user when they exit the game.
Have a think about how you would achieve this,
pause the video and give that a go.
All right. So let's think about this.
What we want to do is to take the guessed states and compare it against all
states,
and then we want to create a new list of all the missing states.
So let's create a new list called missing_states and then we can use a for loop
to loop through each of the states inside all of the states.
And if we find that a state is not in the guessed_states,
well, then that means it's missing, right?
So then we can tap into our missing states and append this state. And
by the end of this,
we should end up with a list of all of the missing states.
So let's test this code. So I'm just going to type in a few states,
some of the ones beginning with A, so Alaska, um,
Arizona,
1: Um, uh,
Arkansas,
0: and what's the last one, Alabama.
Okay. So I think that's all of the states beginning with A,
so now I'm going to hit exit and it's going to give me a list of all the states
that I'm missing. So if we take a look at all the A states,
if we compare it against this list of 50 states, there's no Alabama,
there's no Alaska, and there is no Arkansas because I guessed it correctly.
So this is now just a list of all the ones I didn't manage to get.
So now how can we turn this list into a CSV?
Well,
we can create a new data and that new data is going to be a data frame
which is created from our missing_states.
So this is just going to be a one column data frame.
Once we've created that new data,
then we can go ahead and save it as a CSV.
And we're going to call that states_to_learn.csv,
just as we've typed here. Now,
when I run this code and I've typed
all of the A states, and now I hit exit.
Then you can see we've got a new file
that's just been generated fresh out of the press
and it is our states to learn with all 44 States that I'm missing from my game.
I hope you had fun building this game with me,
and I would love to know how many states you can name the first time you play
this game. Cause obviously the more you play it, the more you get better.
And I hope you'll use this as an educational tool. If you have students,
if you are a geography teacher, if you have kids,
you can get them to play with this awesome program that you've built.
Remember what we've done here can be endlessly customized.
You can make a version for your own country,
or you can make one for the whole world where you have to name each country on
the world map, or you can do some other sort of list,
like name all of the plants on an image or name all of the animals.
The world is really your oyster
and I can't wait to see what you've created using your imagination and how
you've customized the code. So once you've done that,
be sure to post it in the Q/A of this lesson and so that we can all play and
admire your code.