Hey, you've reached the final project.
And as you've seen at the beginning of today's lessons, I showed you what we're going to be making,
which is a sort of, Choose Your Own Adventure Game.
I don't know if you've ever read those,
"Choose Your Own Adventure Books," where you flip to a different page if you make a particular choice,
and then the story kind of evolves as you make your choices.
Now we're going to make a very simple version of that using what we've learned about conditionals,
if, else, elif statements, as well as everything we've done previously.
So you've already seen what the final outcome should look like,
and if you want to review it again, just head over to the link in the URL box or click in the Course
Resources and it'll direct link you here.
And here you'll see essentially the game in action.
So it start off asking you, "You're at a crossroad.
Do you want to go left or right?"
And then you have to type your choice.
If you selected left, then it's going to take you to a lake,
and it asks you whether if you want to wait for a boat or swim across.
So let's go ahead and wait for a boat.
And then the final step is it asks you, which door would you like to go through red, yellow or blue?
Now, depending on your choice, you'll either end up with Game Over or you'll end up with winning
the game.
So the basic setup for this game looks something like this.
And I've included a link to the flowchart in the starting project right here.
And of course, you'll find all the links in the Course Resources as always.
Now, the first question you're going to ask the user is to make a choice between left and right.
Now you can use my wording if you want,
"You're at a crossroad.
Where do you want to go?
Type 'left' or type 'right'."
But I think the fun part of this challenge is really creating your own story, right?
But somehow get them to choose between left and right.
If they choose right or any other condition for that matter,
tell them that they've lost, 'Game Over' for some sort of reason.
In my case, if they had chosen right, the reason is because they fell into a hole,
Game Over.
But I think you're far more imaginative than I am, so go ahead and use all of those skills and make
your game really fun.
Now, if they've chosen left, on the other hand, then we're going to take them to another question
that asks them whether they want to swim or whether if they want to wait for a boat.
And if they swam, then it's going to be some form of Game Over.
There's crocodiles, there's...whatever it may be, but they can't continue.
But if they chose wait, then we take them to the third and final question, which asks them to choose
between three doors and if they choose any door other than the yellow door, then they would lose,
but if they chose the yellow door, then they somehow win.
Now notice how each of these just says, Game Over, Game Over, left or right,
but you know, the idea is really to use your creativity and make this game really your own.
Now, one thing to remember, though, is that when the user types in an answer, let's say left or right,
they might type it like this with an uppercase R, or they might type it like this,
all lowercase.
Have a
think about how you might make sure that no matter which version they typed, you're still going to
be able to catch their command essentially.
So have a think about what you've learned in the previous lessons and take a look at this flowchart,
then head over to the starting project and you can get started writing your code.
Now ASCII art is really cool because it basically just uses a bunch of characters that you know and love,
like the comma and the equal sign and all of these things that together make up a picture. And
you can find your own ASCII art if you just go to, ascii.co.uk/art, and you'll see a whole bunch of different
topics.
And they've grouped a lot of things together into individual pages.
So if you wanted a Rhino ASCII art, then you can find a whole bunch of rhinos that people have drawn.
But the easiest way is, of course, using cmd + F if you're on a Mac, or Ctrl + F if you're on
a Windows machine, and then just search for the thing that you want.
So, for example, I went and searched for treasure, and down here I found this brilliant Treasure
Box in ASCII art, which is what I'm using to print inside this print statement.
Now notice how there's three single quotes that are at the start, and there's three single quotes at
the end.
Basically, what the single quotes allow you to do is to create multiple lines of a string essentially.
So notice how if I go ahead and delete all of these single quotes and I only had a set of double
quotes then it actually doesn't quite work.
It gets quite confused because it thinks that this is where the line ends, but in fact, I wanted
it to print all of this out.
So to do that, instead of using a double quote, I use three single quotes, and at the very bottom,
I tell it well, this is the end of my multi-block string by using three single quotes as well.
So have a go at running that, and you should be able to see that in your console,
you get this little Treasure Island ASCII art being printed along with the two sentences that begin
the game.
So have a look at the flowchart,
have a play around with the ASCII art, and also have a look at the final version of the app so
that you can go ahead and create your own version to complete this challenge.
Pause the video now and give that a go.
All right.
So let's see if we can replicate the functionality or the logic that's shown in this diagram into our
Treasure Island game.
So the first thing it has to do is we have to ask the user whether, if they want to turn left or right.
And after we ask the user this question, we're going to want to capture their input.
Right?
So let's go ahead and create an input() function and ask our question.
Now notice how when I use my double quotes around my string, the double quotes that are inside the
double quotes are interpreted as code, because it actually thinks that this is the end of the first
string.
This is the second string, and this is the final string.
This is how the computer is going to see it.
Now there's a cool thing that we can do in Python.
When we have a string like this, we can enclose a string inside a set of double quotes.
And let's say we have a string in here, "Say the words...
She said: "hello." "
Now, if we want these double quotes to occur and to be viewed as a part of the text, one trick that
we can do is instead of using double quotes to enclose our string, we can actually use single quotes
around it, instead.
And because now we have double quotes inside the single quotes, it will be interpreted as text.
And you can see everything inside here is all green.
Now, if we try to apply this trick to our input on this line here, there's one slight problem.
So let me show you.
I'm going to get rid of the final double quote of our string and the first double quote of our string.
I'm going to highlight the entire string that I would like to be a part of, the input prompt text,
and I'm going to type the single quote button to wrap everything inside.
Now we've solved the problem with the double quotes, but we also have a single quote in our sentence.
So now what the computer thinks you're trying to do is here's a string, starting single quote, ending
single quote.
I've written a bunch of other code, and then I've got some other strings left and right.
And at the end I've somehow forgotten to close that quote.
So how can we fix this?
Well, a really, really simple thing is to simply use the backslash button (\), which is used as an escape
symbol in programming.
So what this says is yes, this is a backslash,
but if the backslash occurs in front of any other useful symbol, then it will mean that we're trying
to escape this symbol,
so don't interpret it as code,
and instead, if we run our code, you'll see the input printout in its entirety.
'You\'re at a crossroad...'with the apostrophe.
'Where do you want to go?
Type "left" or "right".'
And it's exactly what we wanted to happen.
Now, of course, there's other ways that you could have gone around this.
You could have just simply said, "You are at..."at a crossroad rather than using the apostrophe.
But more often than not, you'll need to use a symbol that you'll need to escape,
and you can do that with the backslash.
So that's just a quick tip.
Now at this point, the user is going to type a message, "left" or "right."
So let's go ahead and save that to a variable.
Let's call it choice1, equals their input.
Now remember how I said they could be typing the answer as Right or as right,
so how can we make sure that when we're doing our checks using the if statement we ignore the casing?
Well, one way of doing this is we can simply use the lower() function to change their input, no matter
how they wrote it: Right, right, or RIGHT, it should all be converted into lower case, to this version.
That way we have one thing that we can consistently check for in our if statement, which is coming
up next.
So we can say if choice1 ==, remember the double equals is checking whether if left-hand
side and right-hand side is equal, and the single equal sign is assigning the right-hand side to this
particular variable name.
Now if choice1 equals the string "right", then it's pretty much game over for our player.
So let's go ahead and add our colon and print something like Game Over.
Of course, we can elaborate this a little bit more as to why was it game over?
Well, because you fell into a hole.
This is like the E.T. game from the 80s.
Lots of holes.
so, "You fell into a hole.
Game Over."
But what if they had chosen left instead?
Well, that takes them on to the next question.
So that's the option that will allow them to continue along the game.
Now in this case, we could use an else statement and say, well, if they didn't choose right, then
they probably chose left, right?
But they could have also chosen like an option that was completely not listed.
They could have just written something like this, and it would still trigger the else statement.
So what we should actually do is to switch it around.
So namely, we should say if choice1 is equal to left,
well, in that case then they continue in the game, but otherwise, namely, if they chose right or anything
else for that matter, then it's pretty much Game Over.
So this format makes a lot more sense if you want to continue along the left side of the branch, this
way.
Now, another thing that you might have realized, because we learned about combining different conditions,
is instead of using the .lower() to change the input to lowercase, you could have also said if choice
is equal to left, or if choice1 is equal to left spelled like this, that will both work.
But I think in terms of succinctness and less code writing, I think this makes a lot more sense
to me.
So I'm going to continue with this version.
But if you did it the other way, that's perfectly valid as well.
Now, if their choice1 was left, then they get taken to the next question.
"Do you want to swim or do you want to wait?"
So let's go ahead and continue the game and create another input.
In this input, we're going to ask them a question and say, "You've come to a lake,
there is an island in the middle of the lake.
Type 'wait' to wait for a boat.
Type 'swim' to swim across."
Cool.
So we've actually got the same problem as we had before.
Namely, that this is being interpreted as code rather than as a continuous single string.
So do you remember how we fixed this previously?
And if you got stuck on this, I recommend having a go at this yourself before I show you the answer.
All right.
So previously, we said we were going to switch it to single quotes instead so that these double quotes
become interpreted as just normal strings.
But of course we've got this,
"You've", we could, of course, change it to, you have instead of "you've",
but if we want to escape the string, we just add a backslash, which is exactly what we did previously.
Now, one thing you might notice here is I'm getting a warning here saying PEP 8...
So this is a style guidance.
This line is too long.
And you might have also felt a little bit annoyed having to scroll left and right constantly to be able
to see the end of the line.
So what can we do in this case?
Well, we can actually very easily pick any point in our entirely long string and just hit Enter, and
it will automatically do the right thing and make it go on to the next line.
Now, this doesn't actually change how it gets printed out, because if I go ahead and stop this and
run this code again, you can see it's still typing everything on the same line.
"You're at a crossroad.
Where do you want to go?
Type left or right?"
It doesn't actually affect it at all.
The only thing it does is it helps our code to be more readable.
So we can go ahead and fix this one as well.
So wherever you choose, just hit Enter.
I did it at the end of a sentence because that makes more sense,
but you don't have to.
You can actually even put it in the middle of a character if you really wanted to.
Now here we're going to see a new warning, and it's basically unhappy with how our indentation looks
for this separated second part of the string.
So when you hover over some of these PEP 8 warnings, you can actually just click on this button
for them to do whatever they think is best.
You know, it's like, "All right, you do it, you know, leave me alone and just fix it for me."
So they fixed it for us.
And as usual with AI and machines it now looks like I have to scroll horizontally again.
So let me just fix this so that I put each sentence on a new line so I can see everything in one go
without having to scroll around.
It's quite good for my sanity.
Now, one thing you have to be really careful about is this indentation.
Now, this indentation here doesn't really matter at all.
It's just all for styling.
And the computer was unhappy with having it unaligned with the start of the string.
So that's why they asked us to fix it.
But the thing that really matters is your code's indentation, because this choice2, getting the input
from the user for the second question must be indented inside this if statement, because otherwise
it's going to give you an IndentationError.
And this means that only when the choice for the first question was left do we actually prompt them
for an input and let them continue the story.
And then we get to check our choice2.
So if choice2 is equal to "wait" then it means that the game will continue,
but else,
so if they typed "swim" or anything else for that matter, then the game is going to end.
So we're going to print and tell them that something terrible happened,
"You got attacked by an angry trout.
Game Over."
Now we can continue along this path.
So we're down to here now.
And the final question we're going to ask the user is which door do they want to choose.
So here I'm going to use an input to send them the message that, "You arrive at the island unharmed.
There is a house with 3 doors. One red, one yellow, one blue.
Which color do you choose?"
Now I'm going to save their choice inside a variable called choice3, lowercase, the answer that
they provided,
and finally, I'm going to use if, elif, and else to check which one they chose.
So if choice3 is equal to "red", elif choice3 is equal to "yellow",
elif choice3 is equal to "blue".
And then I've got an else.
So here are four possible things that could happen if they chose red,
then I'm going to tell them that it's game over, but it's going to be some sort of creative version
of Game Over.
So maybe something like, "It's a room full of fire.
Game over."
If they chose yellow, on the other hand,
well, that's going to be the room where the treasure exists.
So I'm going to print
,
"You found the treasure.
You win!"
If they chose blue, they also end up with Game Over.
And I have to give them a different reason.
Now, if you had decided that if they chose anything but yellow, the way that they're going to Game
Over is the same.
Say, if you know, if you chose red, "It's a room full of fire.
Game Over."
If you chose blue, "It's a room full of fire.
Game Over."
Anything else,
it's also going to be fire,
Game Over.
Well, then you could just use if and else.
But if you wanted each choice or each door to have a different outcome for the user, say the red door
has fire, the blue door has beasts,
and if they chose the wrong door, then they're going to get something like, "You chose a door that doesn't
exist.
Game Over."
So can you see that depending on whether if you want to give the user a different print statement,
based on their choice, then you would use if, elif, else,
but if you actually just wanted to give them the same feedback, which is "Game Over", then you could
just use if and else.
Now let's run our game and just make sure that it works.
So I'm going to go left because I know it continues.
And then I'm going to wait for the boat.
And that allows me to get to the final condition,
and if I choose yellow, I know that I'll win.
Now if you want to change the formatting, because having the input at the end of the sentence doesn't
look as good as letting the user type it on a new line, then you can simply, if you remember, just
add the new line which is \n at the end of your inputs, like so.
So how did you get on with this challenge?
And if you didn't manage to do it right, or if you've done it right, go back and try and modify it.
See if you can add some more conditions, or see if you can make the "Game Over" messages a little bit
more interesting than what I've written.
See if you can modify and make it really your own.
Have fun with that.
And in the next lesson, I've got more exciting things coming up for you, so hopefully you've scheduled
some time for tomorrow to complete the next day's lessons.
But for now, it's good night from me.