At this point we've created two while loops.
The first while loop is for the user so that they can continue taking cards, and the second while loop
is for the computer so that the dealer can follow their strategy.
Now it's time to tackle Hint 13, where we create a function called compare().
We pass in the final user_score and computer_score, and then we compare the scores to see who has won,
who has lost, or if they have a draw.
So remember that in order to call a function, it has to be declared before the line
we want to use it.
So let's go back up and let's add our compare() function here.
This compare() function is going to take the user_score and also the computer_score,
and it's going to compare these two scores if the user_score is equal to the computer_score, then we
said this should mean that it's a draw.
So we'll return some sort of message that represents this.
But elif, or else if the computer has a Blackjack,
so the computer_score is equal to 0,
well, in this case, we should return that the user has lost and the opponent has a Blackjack.
And then we go through this list and we add in all of these other conditions.
So I'm going to speed it up a bit.
Now for the final condition,
it says that the player with the higher score should win.
So we can write an elif that compares the user_score against the computer_score,
and we're basically saying if the user_score is greater than the computer_score, well, then that means
that the user wins.
But if that's not true, and because we've covered everything else, we now have the final return,
which is, "You lose".
Now remember that if, elif, else statements are evaluated from top to bottom, even though if the computer
scores a 0, which is a Blackjack, which is obviously much less than whatever score the user might
have, because it's going to return at this point and it's going to exit the function,
it will never get to this comparison if this is already true.
If you get confused about how if , elif, and else works, especially the order in which you add the conditions,
have a look back at the lesson where we talked about this in more detail.
This might be a good time to revise that concept.
Now, another warning that we see in our compare() function is the underlines under the user_score and
computer_score.
And if we hover over them it says, "It shadows the name 'user_score' from outer scope."
What does that mean?
Well, it means that we've decided to use the same name for our variable to be used inside our function
as the name of a variable that we created outside that function.
So how can we prevent this?
Well, we have to rename the element as it suggests.
Now if we click on this then it opens up the Rename window which is going to find everything that's
inside this file to change that variable name.
So let's say that I decide to change it to u_score and Refactor,
you'll notice that everywhere inside that function where I used that variable name has been renamed
automatically for me and the other variables which are unrelated to this particular usage are untouched.
And this is a really nice way to rename things if you ever want to, and you can also get to that manually
by right-clicking on it, Refactor and then rename.
And now you can name it to whatever it is you want.
So in my case I'm just going to call it c_score for computer_score.
And then I'm going to hit Enter,
and all of those changes will be enacted.
But now that we've completed our compare() function, we're going to use it at the end of our code here,
because this is the time that the user is done drawing cards, the computer is done drawing cards,
and now we can call that compare() function and pass in the user_score and also the computer_score.
So this should hopefully give us the final result as a piece of text.
Let's run the code as it is and see what happens.
So we start out with a 3 and a 10 making a score of 13.
We can see the computer's first card is a 3 and we're asked whether we want another card,
so I'm going to type 'y' so that we get another card and closer to 21.
And now we have 3, 10 and 6 making 19.
So I think I'm going to say no to pass and hope that I win.
So we can probably give the user a little bit more information, like revealing the final deck, what
the computer got and what we got.
I'm going to go ahead and create some other print statements above this one.
Maybe I'll tell the user that "Your final hand is {user_card},
final score {user_score}.
And then let's reveal to them the computer_score.
And then finally we can print the user_score and the computer_score to tell them who won, who lost,
or whether if it's a draw.
So in this deck we've got 2 and 8 making 10,
so I'm going to add another card.
Now we're on 16 and I'm going to try and add another card again.
And now I'm on 26,
so the game is very much over.
And it tells me my final hand has a score of 26, the computer's final hand has a score of 20 and I
lose.
If we wanted to play another game because we are Blackjack addicts, then we want to give the user some
sort of means of doing this, right?
And that's what the last hint addresses.
So we're going to ask the user if they want to restart the game,
if the answer is 'yes', then we're going to clear the console and start a new game of Blackjack and also
show the logo from art.py,
while we're at it. Let's go ahead and add an input, and we can ask them, "Do you want to play a game
of Blackjack? Type yes or no."
So remember that this entire function is going to be evaluated,
and the result or the output is going to replace this part of the code.
So we can say that while the input is equal to y, then we're going to keep playing the game.
And the game is all the code between this part where we create our empty variables and where we do our
comparison.
So if we actually put this inside a function by indenting it over and then creating a function here
called play_game(), well then we can actually call this function play game every time the user types
y.
Now we don't want to clutter up our screen with all the previous games,
so before we call play_game() we also want to clear the console. And to do that, we're going to use that
trick that we saw from previous lessons, which is where we print the \n, the new line character,
and we multiply it by 20 times.
And then we can call play_game() so that the code in this function gets repeated.
And we also said that at the beginning when we play_game() we're going to show the user the logo from
art.py.
So let's go back and import that as well.
So from art import logo.
And then at the beginning of every game we're going to print our logo.
That's all there is to this project.
And now let's run it one final time and see the whole thing in action.
"Do you want to play a game of Blackjack?
Type y or n?"
So yes, we've got our logo.
And then it says type 'y' to get another card and type 'n' to pass.
So we're on 19,
that's pretty tenuous.
I'm going to pass and I end up with a score of 19,
the computer, unfortunately, has gone over 21, so therefore, I win. And we can keep repeating this to
play more games of Blackjack.
So I hope you had fun at building this game with me.
I know this was a really difficult project, and I know that some of you would have probably had some
issues, but I really hope that you persevered and you got through it in the end and through the process,
you've managed to apply a lot of the things that we've learned in the last ten days.
This is a good time to review the concepts you've learned, and have a think about what you might need
to revise or revisit so that you're really, really strong on all the concepts and you're comfortable
to continue on to the next ten days.
Now, if you want to take a look at the solution code I've got here, then simply head over to this solution.py,
and you will see everything that I've written in here for you to be able to browse and refer
to.
So now rest up.
Well done for finishing this day and if you haven't completed it, don't worry, there's always another
day tomorrow.
You can always come back and eventually, with deep perseverance, you will get there in the end.
I believe in you.