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 after hint number six
where we had the last function,
let's add this hint here and create 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 zero, 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 gonna speed it up a bit.
Now for the final condition,
it says that the player with the highest 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 score is a zero 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.
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 a time that the user is done drawing cards,
the computer's 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.
We started out with a 3 and a 6 making a 9.
Now we know the computer at least got a 10, so let's add another card.
So now we have a score of 11 with 3, 6, and 2,
so we can still keep going. And luckily we've managed to get a total of 21.
Now we're pretty much invincible I think. So if I go ahead and hit no
cause I don't want to take any more cards, then let's see what the result is.
So it tells us we win and a big smiley face.
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 this,
and the final score was...
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 its a draw.
In this deck, we've got 14, so let's add another card to it.
Unfortunately we went over because we've got another 10.
So our final hand is 10, 4, 10
and the final score was 24. The computer's final hand was 21,
lucky them.
So we went over and we lost. 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 a input and we can ask them,
do you wanna 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.
The game is all the code between hint 5 and the end where we compare.
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 obviously have to import the clear function from the replit module.
So from replit
import clear, and now we can call this clear function to clear the console in
here after each game.
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 wanna play a game of Blackjack? Yes.
And it reveals our beautiful Blackjack logo.
It tells us the cards that we have, so current score of 11.
I'm going to take another card. I'm on 21, which is perfect.
So I'm going to pass now. And it tells me that my final hand was 21,
the computer's final hand was 17 and I win. And now I can play another game just
by typing 'y' again.
And I can keep going on and on playing my game 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 10
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 10 days. Now,
if you want to take a look at the final code that I've got here,
then just head over to repl.it/@appbrewery/blackjack-final
and you'll see all the code that I went through with you just now.