Right. So how did that go?
I hope it went really well and you've managed to complete it all by yourself.
If you got stuck, I really recommend just having a sandwich, have a cup of tea,
take a break, and then come back to it.
You'd be surprised what your brain can do when it just has a little bit of rest.
But I want you to make sure that you've really given it a proper go before you
come here and watch the solution.
Cause I'm going to walk you through the solution step by step in case there are
little bits or something that you didn't understand or something that you want
to check. But I really,
really hope that you've managed to tackle most of this yourself. All right.
So without further ado, let's get started coding up our Blackjack project.
Hopefully you've already used hints 1, 2 or 3,
and I'm going to get started with hint 4 and build up our Blackjack project
step-by-step. Now the idea of the hints 1, 2,
and 3 is to get you to create these small to-dos of your own so that you
can have practice breaking down the problem into a list and then breaking it
down into a flowchart and then breaking it down into smaller bite-size to-dos.
So let's start out with the first one,
we have to create a deal_card function that uses the list below to return a
random card and 11 is going to be the Ace in this deck.
So let's get started by forking a copy of this repl and we're going to go and
create our deal_card function.
Now this function is going to contain these cards,
so I'm going to go ahead and paste it in, make sure it's indented
so it's inside the function. And now I'm going to use these cards to randomly
pick one out of this list. So to do that,
we can of course import our random module.
And then I'm going to use random.choice to pick a random card from my list
of cards. And then this of course has an output,
this random.choice
so I'm gonna save that output, namely the random card, inside a variable called
card.
And then finally we're going to return this chosen card as the output.
So that's step 4 done.
Now let's move on to step 5.
So we're going to deal the user and computer two cards each using the deal card
function, and very helpfully
the hint tells us that we can start out with a blank
user_card and a blank computer_card list.
So we basically just have to add two cards to
each of these lists. To do this,
I'm going to use a for loop and I'm going to use the range operator to run this
for-loop twice.
So you've seen this syntax before when we talked about for loops and I've got an
underscore here because we don't actually need this particular variable.
All we need is for this loop to run twice
and this code is going to achieve that. Every single time this loop runs
we're going to get a new card by calling deal_card.
Now remember that deal_card has an output,
it outputs a random card. So if you want,
it's actually quite helpful to add some documentation to this.
So we could say returns a random card from the deck.
And now when I write deal_card
you can see that the doc tells me that this function is going to return a random
card from the deck. So that random card is going to replace this function call
so we can capture that data by storing it inside of a variable.
So we'll call that new_card and set it equal to the output from the function
deal_card. Now,
the next thing we're going to do is we're going to add this new_card to the
user's cards.
So at the moment, it's an empty list and all we have to do is say user_cards
.append
and then the object that we want to add to that list is going to be the new_
card.
Now some of you might be tempted to instead use the plus equals.
You might want to write something like this; user_cards += new_card.
And I want to show you what actually happens when yourun this.
cause this is a really common error.
We get a trace back and we get a type error.
It tells us that the int object is not iterable and it highlights this line 49
as the reason for the error. So what's going on here?
Well,
this += is actually shorthand for writing the extend function.
Now you can see that the extend function is something that extends a list
by appending elements from the iterable. So what does this mean? Well,
it means that whatever you put inside this parentheses,
it has to be a list itself. For example,
if this new_card was a list of cards,
so maybe just the first dealt card and another card
or even just it by itself inside a set of square brackets
which turns it into a list, well then if we run this code right now
we no longer have any errors and if I convert this back to what you saw before,
which is using the plus equals, we also have no errors.
But when you only want to add a single item, not a list, to an existing list
then you have to use append. So hopefully you didn't have this issue,
but if you did,
this is the reason. Now that we've created these two lines,
you can see that it's a little bit redundant because we're creating this new
card variable just to hold the output from this function.
And then we're immediately putting it into this list using the append.
So instead we can actually just get rid of the middleman and put deal_card
directly inside the append.
And this does exactly the same thing in a single line of code.
Once we've done this for user_cards,
we'd probably want to do the same thing for the computers cards.
So it computer_cards.append, and then we call deal_card again,
remembering the parentheses.
So we've got a total of three parentheses at the end and this way we deal a new
card and that new card gets appended to the end of this
list.