Now let's move on to hint number 10.
It says that if the game has not ended ask the user
if they want to draw another card. If yes
then use the deal card function to add another card to the user's card list.
If no, then the game has again
ended. Over here in the if statement
we can add an else because if the game hasn't ended, as in
they haven't gone over 21 or nobody's got a Blackjack,
then we're going to ask the user if they want to get another card.
I'm going to use a input
and I'm going to ask the user to type 'y' to get another card,
otherwise type 'n' to pass.
And I'm going to save this input inside a variable, which I'll
call user_should_deal.
Now we can check to see if this variable is equal to 'y'. Well in that case,
we're going to add another card to
the user's card list by doing the same thing that we did in the beginning
where we dealt two cards to each player by saying
user_cards.append and inside the parentheses
the thing that we want to append is the output from deal_card.
So just a quick check to make sure you've got all the parentheses there and then
we're ready to address the else. If they didn't type 'y',
then that means they don't want another card. Well, then in this case,
the game has again ended.
So we can say is_game_over equals true.
Let's test this out as well.
We got 7 and 11 with the current score of 18.
If we type 'y' hopefully we'll get another card.
And we did see that prompt and we saw something happen,
but our program then promptly ended. Let's take a look at the next step.
How can we get our game to repeat itself?
So hint number 11 tells us that the score will need to be rechecked with
every new card drawn
and the checks in hint 9 needs to be repeated until the game ends.
Notice how we've got this flag, is_game_over, which is set to true
whenever the game ends. It starts off being false
and what this means is we can actually create a while loop
which basically keeps on calculating the user_score
and the computer_score, makes all of these checks
which are in hint 9,
and then repeats itself until the game ends.
Let's go ahead and do that. After all the initial setup,
we're going to create a while loop and the while loop is going to be active
until the game is over. So we can say
while not is_game_over,
then go ahead and carry out all of these instructions.
So calculate the user_score, calculate the computer_score,
check that nobody's gone over 21 and nobody's gotten a Blackjack.
And if the user wants more cards than they should, if they don't then the
the game is over. So let's run this and try it again.
Our cards are 10 and 10 which probably means we shouldn't get another card
cause we're already so close to 21, but I'm going to be daring and press
'y' anyways. And it tells us that our cords are now 10,
10 plus two.
So we got an extra card and that takes up our score to 22.
That worked perfectly.
So now let's go back and tackle the next part. Hint
number 12 says once the user is done, it's time to let the computer play.
The computer should keep drawing cards as long as it has a score less than 17.
So the while loop is responsible for dealing with when the user wants to keep
drawing cards. But once that's done,
then we can tackle hint number 12,
which is how does the computer actually play? What is its strategy?
Well, we know that the computer has to keep drawing cards
as long as it has a score of less than 17. But, of course,
0 which represents a Blackjack is also less than 17,
but we don't want it to draw a card if it has a Blackjack.
We can represent this logic using a while loop; while the computer_score is not
equal to zero and the computer_score is less than 17,
then in this case we want to keep drawing cards.
We're going to take the computers cards
and we're going to use the append to add a card by dealing another card to the
computer.
And then we're going to recalculate the computer_score so that it updates
and this while loop is evaluated on the latest score.
So the computer_score is going to equal calculate_score using the computer's
cards.
And that means that the updated computer_score is equal to the return value from
calculate_score. So that's hint 12 complete. In the next step
we're going to start comparing our player's hand with the dealer's hand.
See you in the next lesson.