Now let's move on to a hint 9. So we want to call
calculate_score at some point in our code
and if the computer or the user has a Blackjack
or if the user's score is over 21, then the game is going to end.
This might be a good time to start tidying up our code a little bit,
because we've got our function over here,
the calculate score function.
And remember that you can only call a function after it's been declared.
So for example, if I wanted to call this function over here,
then I actually can't.
I can't say calculate_score because it hasn't yet been declared.
So instead,
what I'm going to do is I'm going to take this function and I'm going to move it
to where we dealt our cards and I'm going to collapse some of these functions so
I can see my hints and my code a little bit easier.
So now that I've moved my function to above the line where I want to call it,
it's now become valid code.
And this is the place where I want to call it because it's only after I've dealt
the user and the computer some cards can I actually calculate their scores.
Firstly, I want to calculate the score using the user's cards.
So this list of cards from the user gets passed into this function and uses
all of the logic to output some sort of score.
And then I'm going to store that score in a variable
which I'll call user_score.
And I'm going to do the same thing for the computer,
so computer_score equals calculate_score. And remember,
it's always helpful to add a little bit of a docstring to tell ourselves and
other programmers what this function does. Let's quickly summarize it.
So this function is going to take a list of cards and return the score
calculated from the cards.
So now when I open that parentheses,
I know that I probably have to pass in my list of cards in order to get back the
score.
So let's put in the computer_cards in as the inputs,
and now we've got the user_score and the computer_score.
So this score could equal zero
if they got a Blackjack or it could just simply be the value of the cards that
they hold all added up together.
Now that we've called calculate_score
we also want to make sure that if the computer or the user has a Blackjack
or if the user's score is over 21, then we have to end the game.
Let's write our if statements. If the user_score is equal to zero,
or the computer_score is equal to zero,
or the user_score is greater than 21, then in this case
we're going to tell the game to end.
So we could create a new variable called is_game_over
and we start out with false, of course. But then when this happens,
we're going to change that variable to true instead.
Our is_game_over variable is just a simple boolean.
It starts out as false and when certain conditions are met,
then we change it to true. Now at the moment, our program is not finished.
So it was not really obvious how we're going to be using this is_game_over
variable. But we're setting ourselves up for the next steps.
Now that we're tracking if the game should end,
we can look at this value to determine what to do next.
So that's hint 9 completed. Let's test our code and see how everything works so
far.
I'm going to add some print statements here so that I can see what the user's
cards are and what the user's score is. Using an fstring
I'm going to show your cards, so the user's cards, and the current score.
And I'm also going to print the computers first card.
Remember when I first explained the rules of Blackjack,
the dealer will reveal their first card
so you get a little bit of a clue as to what kind of hand they might have.
So in here, we're going to insert the computer's cards
and we're only gonna pick out the first item, so that the one at index zero,
and we do that using a square bracket after the name of the list.
Now let's give our program a run and see if it actually works.
The first thing that prints is a list of our cards.
So in our hand,
we have a 2 and we have a 6 and 2 + 6 added together
of course makes the score of 8.
And then it reveals to us the computer's first card, which is a 10.
Now we can be reasonably assured that our code is working.
And it's a good idea to regularly test your code so that you don't wait until
the end when they're a lot of problems
and you don't know which part of the code is responsible.