Category
📚
LearningTranscript
00:00In this video, I'm
00:08going to teach you
00:09Python by working
00:10through a project. Now,
00:11this project I'm going
00:12to write completely
00:13from scratch, which
00:14means I don't have it
00:15up on my other screen.
00:16I've not written this
00:17before. It's a
00:17completely new project
00:18to me. Now, the point
00:19of this is not only am
00:20I going to teach you
00:21basic Python syntax
00:23and some different
00:23language features, I'm
00:25also going to explain
00:26to you how you
00:26structure a program,
00:28how you've decide
00:29where you're going to
00:29start, what different
00:30components you need to
00:31build out and kind of
00:32how you go about the
00:33different tasks. This
00:34is going to be perfect
00:35for people that have a
00:36little bit of experience
00:37with Python. They've
00:38written some code
00:39before, but they're not
00:40yet comfortable and
00:41confident to go and
00:42write their own
00:42project where they don't
00:44know where to start
00:44when they're kind of
00:45diving into a task on
00:46their own. Hopefully
00:47this is going to make
00:47you a lot more
00:48confident. You're going
00:49to learn a bunch about
00:50Python and you're going
00:50to have a good project
00:52that you can show
00:52after this video. So I
00:54encourage you to follow
00:54along, type this
00:55project out with me.
00:57And with that said,
00:57let's dive in after a
00:59quick word from our
01:00sponsor. Before we get
01:01started, I need to thank
01:02OctoML for sponsoring
01:04this video. As we know,
01:05building and training,
01:06the perfect deep
01:07learning model for your
01:08application is just the
01:09first step. After that,
01:11it's time to push it to
01:11production. Now, rather
01:13than learning completely
01:14new tools for model
01:15acceleration and
01:16containerization, you
01:17need a bridge that gets
01:18you from model building
01:19and training to
01:20deployment as fast as
01:21possible. Now that's
01:23where OctoML CLI comes
01:25in. Using OctoML is very
01:27similar to working with
01:28Docker and Kubernetes.
01:29You start with a trained
01:30model and ingest it into
01:32OctoML. Next OctoML
01:34packages the model into
01:35a Docker container with
01:36Nvidia's Trident inference
01:38server, which you can
01:39deploy to your local
01:40Docker desktop, any
01:41cloud Kubernetes service
01:42like Amazon EKS, Azure
01:44AKS, or even managed ML
01:46services like Amazon
01:48Sage maker. When
01:49you're ready, you can
01:50then use OctoML to
01:51accelerate your model
01:52peak performance on your
01:53CPU or GPU hardware
01:55target. Get started
01:56today by clicking the
01:57link in the description
01:58and downloading the
01:59OctoML CLI for free.
02:01Make sure to be one of
02:02the first 20 tech with
02:03Tim subscribers to try
02:05OctoML to receive a
02:06special welcome gift.
02:07So you're probably
02:08wondering now, what
02:09kind of project are we
02:10going to work on now? I
02:11wanted to pick something
02:12that was unique and that
02:13I haven't done before.
02:14So we're actually going
02:15to build a text based
02:16slot machine. Now, the
02:17way this will work is
02:18the user will deposit a
02:19certain amount of
02:20money. We're then going
02:21to allow them to bet on
02:22either one, two, or
02:23three lines of the slot
02:24machine just to keep it
02:25pretty simple. I know
02:26in real slot machines,
02:27they have a lot more
02:28lines than that. And
02:29then we are going to
02:30determine if they want.
02:31So if they got any
02:32lines, we're going to
02:33multiply their bet by
02:34the value of the line,
02:36add that to their
02:36balance, and then allow
02:37them to keep playing
02:38until they want to cash
02:39out or until they run
02:41out of money. So this
02:42is actually a fairly
02:42complex project because
02:44we need to do a lot of
02:44things. We need to
02:45collect the user's
02:46deposit. We need to add
02:48that to their balance.
02:49We need to allow them to
02:50bet on a line or on
02:52multiple lines. We then
02:53need to see if they
02:54actually got any of
02:55those lines. We then
02:56need to spin the slot
02:57machine, or we would
02:58have done that before,
02:59right? We need to
03:00generate the different
03:01items that are going to
03:02be in the slot machine
03:03on the different reels.
03:04And then we need to add
03:05whatever they won back
03:06to their balance. So
03:07there's quite a bit of
03:08stuff going on here, and
03:09this should be a
03:10challenging enough
03:10project to make it
03:11interesting for you
03:12guys. So before we even
03:13get into this, I just
03:14want to mention, I
03:15obviously am not
03:15supporting gambling
03:17here. This is just kind
03:17of a cool thing that we
03:18can do. We're not using
03:19any real money and I
03:20think it's a good
03:21project. So just keep
03:22that in mind. Do not
03:23gamble. I'm not
03:23supporting that by
03:24making this video. And
03:26before I write any code,
03:27what I like to do here
03:28is just think of the
03:29different things that
03:30we need to do in this
03:31project and kind of
03:32pick a good starting
03:33point. The place where
03:34I like to start usually
03:35is collecting some user
03:36input. And for this
03:37project, we need to get
03:38the deposit that the
03:39user's entering as well
03:41as their bed, right?
03:42Those are kind of the
03:43two things we need
03:43before we can start
03:44actually using the slot
03:45machine, uh, and
03:47generating, you know,
03:48whatever the slot
03:49machine spun. So to do
03:50this, I'm going to make
03:51a function here called
03:53deposit. Now I'm just
03:55inside of visual studio
03:56code. You can work in
03:57any, any editor you
03:58want. Sorry. And I have
03:59this main.py function
04:00open. I assume you guys
04:01can open your own
04:02Python file and kind of
04:03work in your, um,
04:04preferred environment.
04:06So this function here is
04:07going to be responsible
04:08for collecting user
04:09input that gets the, uh,
04:10deposit from the user.
04:11Now function, if you're
04:12unfamiliar, it's just
04:13something that we can
04:14call. That's going to
04:15execute a certain block
04:16of code and then can
04:17potentially return us a
04:18value. And you'll see
04:19how this works in a
04:19second. So what I'm
04:21going to do here, set up
04:21a while loop. And the
04:22reason I need a while
04:23loop is because I'm
04:24going to continually
04:25ask the user to enter a
04:26deposit amount until
04:28they give me a valid
04:28amount. So if they
04:29don't give me a valid
04:30amount, then they need
04:30to keep typing in until
04:32eventually we get one.
04:33So to do this, I'm
04:34going to say my amount
04:35is equal to input. And
04:37then inside of input,
04:38I'm going to pass a
04:39prompt, which is some
04:40texts that will happen
04:41before we allow the
04:42user to start typing.
04:43So for the prompt, I
04:44can say, uh, what would
04:47you like to deposit
04:50question mark? And then
04:51I can put something
04:52like a dollar sign and
04:53then allow them to
04:54start typing after the
04:55dollar sign. Okay. Now
04:57what I need to do is
04:59check if this amount is
05:00actually a number
05:02because they could type
05:02in anything here, right?
05:03They could type in a
05:04low world. They could
05:05type in nothing and
05:06just hit enter. They can
05:07do whatever they want.
05:08So I need to make sure
05:09that the amount
05:09actually is a number
05:10before I get out of this
05:11wallet. So I'm going to
05:13say, if the amount dot
05:16is, and then digit like
05:18this, then we can do
05:20something inside of
05:20here. Now is digit is
05:22just going to tell us if
05:23this is a valid whole
05:25number. So if it's
05:26something like a
05:26thousand, you know, 10,
05:28whatever, if they type
05:29in a negative number, it
05:30actually won't be true.
05:32So if they do something
05:32like negative nine, then
05:34it's not going to be
05:35true. And we call his
05:36digit. Anyways, this is
05:37a method you can use on
05:38strings to determine if
05:39this is a valid number.
05:41So if this is a digit,
05:42then what I'm going to
05:42do is I'm going to
05:43convert this into, and
05:46uh, so I'm going to say
05:47amount is equal to int
05:48amount. And that's
05:49because by default, this
05:50comes as a string and we
05:52want to have a numeric
05:53value for our balance or
05:54for our deposit. So we
05:56need to convert this to
05:57an end. However, I can't
05:59do this before I make
06:00this check, because if I
06:01do that, then this could
06:02potentially fail. Cause
06:03if they try to type in
06:04something like hello
06:05world, then this here is
06:07invalid. I can't convert
06:08hello world into an
06:09integer. So that's why
06:10I'm first checking if
06:11they did actually enter
06:13a valid number. If they
06:14did, I then convert it
06:15to a number. Now, what I
06:16need to do is check if
06:17this number is greater
06:18than zero. So I'm going
06:19to say, if the amount is
06:21greater than not equal
06:23to, but is greater than
06:24zero, then I'm going to
06:25break out of this
06:26wallop. If it's not
06:28greater than zero, then
06:29I'm going to put an L
06:29statement here and I'm
06:30going to say, print
06:33amount must be greater
06:36than zero dot. Okay,
06:38there we go. Now I'm
06:39just going to add one
06:40more L statement down
06:41here and I'm going to
06:42say, print, uh, please,
06:46if we could type this
06:47correctly, enter a
06:48number. Okay. Then
06:50finally here, we are
06:52going to return our
06:54amount. All right. So
06:55let me go through what we
06:56just did here. So we
06:57wrote a function
06:58deposit. This is a wall
06:59loop. So we're going to
07:00continue to do this
07:01until eventually we
07:02break out. And this
07:03break keyword just ends
07:03the wall loop. And then
07:04we'll bring us down to
07:05this line. I ask, what is
07:07the amount you want to
07:08deposit? They enter
07:09something. If this is a
07:11digit, then I'm going to
07:12convert this to an
07:13integer. And I'm going
07:13to check if this is
07:14greater than zero. If it
07:16is, it's a valid amount.
07:17We can work with this.
07:18We'll break out and
07:19then we'll return that
07:20amount and we can use
07:20that later on. Okay.
07:22Otherwise I'm going to
07:23print. This amount must
07:24be greater than zero
07:25because if it's not
07:25greater than zero, but
07:26we need to tell them
07:27that and then get them
07:28to try again. All right.
07:29So that handled this
07:30first if statement. Now,
07:31if the amount is not a
07:32digit, then what we're
07:33going to do is, uh,
07:34print, sorry, please
07:35enter a number. And
07:37we're going to continue
07:37this until we get a
07:38number and until we
07:38break. All right. So
07:39that was that function
07:40now to call the
07:41function. If we spell a
07:44deposit correctly here,
07:45I don't even know how I
07:46messed that up so
07:47badly. Okay. Deposit,
07:49uh, to call the
07:49function, we simply
07:50write the name of the
07:51function and we put our
07:51two parentheses. And
07:53now, as soon as I run
07:53my code, this function
07:55will run and we'll be
07:56able to actually enter a
07:57number. So let's do
07:57this. You can see in my
07:59terminal that it's
07:59asking me, what would
08:00you like to deposit?
08:01Let's go with something
08:02like $10 and all is
08:04good. Now, if I run this
08:05again and I enter
08:06something like hello
08:07world, it tells me
08:08please enter a number.
08:09Okay. Let's enter zero.
08:11All right. Amount must
08:12be greater than zero
08:13enter one. I'm good to
08:14go. So there we go.
08:15We've written our first
08:16deposit function and
08:17I've tested it. It's
08:18working good. We're all
08:19good to go. All right.
08:20So now what I'm going to
08:21do is I'm going to say
08:22my amount is equal to my
08:24deposit. And actually
08:26we're going to call this
08:27balance. And this is
08:28kind of the start of our
08:29program here. And in
08:30fact, I'm going to put
08:31our program in this
08:32function main so that if
08:34we end our program, we
08:35can just call this
08:36function again, and then
08:37it will rerun the
08:38program, right? So I can
08:38say, do you want to play
08:39again? And then we can
08:40rerun the main
08:41function and we'll be
08:42good to go. So we've
08:43done that. Now I need to
08:45call the main function
08:46so that we start running
08:47main and then it's going
08:48to do everything inside
08:48of here. Okay. So now
08:50inside of me, the next
08:51thing that I want to do
08:53is I want to collect the
08:54bet from the user. So
08:56there's actually a few
08:56ways I can do this, but I
08:57need to determine how
08:58much they want to bet.
08:59And then how many lines
09:01they want to bet on. And
09:02then I would multiply
09:03their bet amount by the
09:04number of lines. So it
09:05probably makes sense to
09:06ask the number of lines
09:07first and then ask them
09:08how much they want to
09:09bet on each line. So
09:10it's not confusing. If I
09:11say how much you want to
09:12bet, they put $10 and
09:14then they say three
09:14lines and ends up being
09:15$30. See what I mean? So
09:17let's say define, we'll
09:18say, get number of lines
09:22like this. And inside of
09:23here, we're going to ask
09:25them to pick a number
09:26between one and three,
09:28because that'll be the
09:28number of lines that we
09:29have. And to keep this
09:30program nice and
09:31dynamic, I'm going to
09:32add, what's known as a
09:33global constant at the
09:35top of my program. And
09:36I'm going to say max
09:38underscore lines is
09:39equal to three. Now I'm
09:41doing this in all
09:41capitals because this is
09:43a constant value,
09:44something that's not
09:45going to change. And
09:46this is a convention in
09:47Python. You do it in all
09:47capitals and making it
09:49equal to three. Okay. So
09:50now anywhere in my
09:51program where I'm
09:52referencing the number
09:52of maximum lines in my
09:54slot machine, rather
09:55than writing three, I'll
09:55just write this. And then
09:57later on, I can easily
09:58change this to be five
09:59and the whole program
09:59will update based on
10:00that. Right? So let's
10:01make this three for now.
10:02Okay. So get number of
10:03lines. Obviously the
10:05minimum lines will be
10:05one. We don't need to
10:06write that in. Okay. So
10:07we need to ask them to
10:08enter this so we can
10:09actually just copy
10:10everything we have in
10:11the deposit function and
10:12just change a few
10:13values here. So we're
10:14going to say while
10:15true, rather than
10:16amount, we're going to
10:17say lines and we'll say
10:20enter the number of
10:23lines to bet on. Okay.
10:26And then I want to put
10:27inside of here, kind of
10:28the options. So like one
10:29to three. So I'm going
10:30to write one dash, and
10:32then I'm actually going
10:33to do a concatenation
10:34here plus, and then I'm
10:35going to convert my
10:37number of lines or my
10:38max number of lines to a
10:40string. And then I'm
10:41going to say plus, and
10:43then I'm going to put a
10:44ending bracket here,
10:45question mark, and then
10:46a space. Okay. I know
10:47it seems a little bit
10:48confusing, but what
10:49I've just done here is
10:50I have now added max
10:51lines inside of this
10:53string. There's a few
10:54other ways to do this,
10:54but this is the most
10:55basic. So that's how I'm
10:56doing it. And I need to
10:57convert this to a
10:58string because if I add
10:59two strings together,
11:00they get squished
11:01together. But if this
11:02was a number and I tried
11:03to add it to the string,
11:04then this would cause an
11:05exception in my
11:06program. So enter the
11:07number of lines to bet
11:08on one dash, and then
11:09I'm going to put in
11:10whatever the string
11:11version is of the
11:12maximum number of lines
11:13and my bracket put a
11:14question mark. And then
11:15I'm putting a space so
11:16that when they start
11:17typing, they're not
11:18like connected to the
11:19question mark. They're
11:20off by one space and it
11:22looks easier for them to
11:23read. Right? Okay. So
11:24now same thing I need to
11:26make sure that they
11:26actually did enter a
11:27number. So I'm going to
11:28say if lines dot is
11:29digit, then I'm going to
11:30say that my lines is
11:32equal to int lines like
11:34this. And I'm going to
11:35check now if the lines
11:37is within the bound that
11:38I had. So within one and
11:40three. So the way to do
11:41this is I'm going to say
11:42if, and I'm actually
11:43going to say zero, um,
11:45actually we'll say one
11:47less than or equal to
11:48lines less than or equal
11:51to, and then this will
11:53be the max lines. All
11:55right. So this is
11:55something you can do in
11:56Python. You may not have
11:57seen before. If I want to
11:58check if a value is in
11:59between two values, I
12:00can write it like this.
12:01So what I'm saying here
12:02is if my lines is
12:03greater than or equal to
12:04one and is less than or
12:05equal to the maximum
12:06lines, then I'm okay. I
12:07can break. Otherwise I
12:09need to tell them to
12:10enter a valid number of
12:11lines. I'm going to say
12:11enter a valid number of
12:14lines. Okay. Otherwise
12:16same thing. We'll say
12:16enter a number and then
12:18rather than returning
12:18the amount, we'll return
12:20the lines. Okay,
12:21perfect. So now we have a
12:21function that gets the
12:23deposit amount and the
12:24number of lines. So now
12:25in our main function,
12:26we'll say lines is equal
12:27to get number of lines
12:28like that. And now what
12:29we can do is print out
12:31the balance and the
12:33lines, just so we can see
12:34what this is when we run
12:35the program. Okay. So
12:36now let's go here. Let's
12:37run. And how much would
12:39you like to deposit?
12:40Let's go with a hundred
12:41dollars. Okay. Enter the
12:42number of lines. Let's
12:43try four. Okay. It
12:44doesn't work. Let's try
12:45zero. It doesn't work.
12:46Let's try H E doesn't
12:48work. Let's try to
12:49valid. And notice I now
12:50have a balance of a
12:51hundred and the number of
12:52lines is two. The next
12:53thing I need to get from
12:54my user input is the
12:55amount that I want to
12:56bet on each line. So
12:58let's do this and we'll
12:59have a maximum bet as
13:01well. So we'll say max
13:02bet is equal to let's go
13:04with something like a
13:05hundred dollars. And
13:06let's say the minimum
13:06bet is equal to $1. And
13:09again, we're putting
13:09these as constants so
13:10that we can use them
13:11kind of anywhere in our
13:11program. So now let's
13:13just write this
13:13function from scratch
13:15and say, define get
13:16bet. So we can add our
13:17colon and continue and
13:19forget that. We'll just
13:20ask them how much they
13:21want to bet. Uh, and
13:22honestly, actually, now
13:23that I think of it,
13:23let's just copy the same
13:24thing from deposit. I
13:25don't like repeating all
13:26of this code, but there
13:27are slightly different.
13:28So it's fine to do
13:29something like this. So
13:30for the amount, we can
13:30actually use the same
13:31value. And rather than
13:32what would you like to
13:32deposit? We'll say,
13:33what would you like to
13:34bet? Question mark
13:35again, we're going to
13:36check if the amount is a
13:37digit. We're going to
13:38convert this now to an
13:39integer. And now we need
13:41to check if the amount
13:42is between the minimum
13:42and the maximum bet. So
13:44I'm going to say the
13:45min bet less than or
13:47equal to the amount less
13:48than or equal to the max
13:49bet. Okay. And then
13:51break. And here, I'm
13:53going to say the amount
13:54not must be greater than
13:55zero amount must be
13:58between. And now I'm
13:59going to show you a
13:59second way that we can
14:00actually, um, kind of
14:02put variables in our
14:03string. So I'm going to
14:04use an F string here only
14:05available in Python 3.6
14:07and above. I'm going to
14:08say amount must be
14:09between. I'm going to
14:10put my, uh, sorry,
14:12squiggly brackets like
14:13this, and I'm going to
14:14say min bet and then max
14:17bet. So this is actually
14:18a very easy way to, um,
14:20embed values inside of
14:23your strings. You put F
14:25before the string, and
14:26then you put your curly
14:27braces like this. And
14:28inside of the curly
14:29braces, you can write any
14:30variable and it will
14:31automatically be
14:32converted to a string for
14:33you if it can be
14:34converted. So in this
14:36case, I don't need to
14:36convert min bet to a
14:37string or max bet. It's
14:39automatically going to
14:40get converted by Python.
14:41And I've just put my
14:42dollar signs here to make
14:43it look a little bit
14:43better when I say, you
14:44know, between the min and
14:45the maximum bet. Okay.
14:47Uh, otherwise please
14:48enter a number. That's
14:48fine. And then return
14:49amount. Okay, perfect.
14:50So now I'm going to say
14:52bet is equal to, and
14:54then this is going to be
14:56the get underscore bet.
14:59All right. Uh, now we
15:01should actually make
15:01this a bit more clear.
15:02What would you like to
15:03bet on each line? Okay.
15:06So now that we've done
15:07this and we've gotten
15:09the deposit, gotten the
15:10number of lines and
15:11gotten the bet, we're
15:12probably want to print
15:13out here in our main
15:14function, kind of what
15:15they've said so far.
15:16Right? So we'll say you
15:18are betting, you know,
15:20$5 on three lines. Your
15:21total bet is $15,
15:23something like that.
15:24Right? So we're going to
15:25say print, you are
15:27betting, and let's do an
15:29F string here as well.
15:30And then let's do a
15:31dollar sign. And we're
15:33going to say whatever
15:33the bet is, we're going
15:34to say you are betting
15:35whatever the bet amount
15:36is on. And then this is
15:39going to be lines like
15:42this. And then we'll
15:43write lines and we'll
15:45say total bet is equal
15:49to, and then we'll put a
15:51dollar sign and we're
15:52going to put inside of
15:53here, a variable. I'm
15:54going to say total bet
15:55is equal to bet
15:56multiplied by the lines.
15:57Okay. And we'll say this
15:59is the total bet. All
16:01right. So let's run this
16:02now and let's see what
16:04we get. We no longer
16:04need to print out the
16:05balance in the lines, by
16:06the way. All right. So
16:08let's run this. How much
16:10would you like to
16:11deposit? Let's say a
16:12hundred dollars. Enter
16:13the number of lines to
16:13bet on. Let's go with
16:14two. Would you like to
16:16bet? How much would you
16:16like to bet on each line
16:17or what would you like
16:18to bet on each line?
16:19Let's go with $10. And
16:21now it says our total
16:22bet is $20. Great. So
16:24this is okay, but I just
16:25realized that we actually
16:25need to check if the
16:26amount that they're
16:27betting is within their
16:28balance because they
16:30can't bet more than
16:31whatever their current
16:31balance is. So we need
16:33to check that. And we
16:34could check this in a
16:35few different places,
16:36but since I've called
16:37this get bet, I'm not
16:38going to put it inside
16:39of here. I'm just going
16:40to do the check here.
16:41And then I'm going to
16:42recall, get bet if they
16:44entered an invalid. So
16:46I'm actually going to
16:46put now this inside of a
16:48wallet, but I'm going to
16:48say, well, true. I'm
16:50going to say bet is
16:51equal to get bet. And
16:52I'm going to then say
16:53total bet like this
16:54inside of here. So we
16:55put total bet. Okay. Now
16:59I'm going to say if my
17:01total bet is greater
17:03than my balance, then
17:05we'll say print. You do
17:10not have enough to bet
17:16that amount. And then
17:18we'll say what their
17:18current balance is your
17:20current balance is. And
17:24then we'll put dollar
17:24sign. We're going to put
17:26our F string here, and
17:28this is going to be
17:30their balance. All
17:31right. And then
17:32otherwise we will simply
17:34break out. Okay. So
17:36hopefully that makes
17:37sense. I also could just
17:38put a condition here for
17:40the wallet, but I think
17:41this is okay for right
17:41now. All right. So let's
17:43try this now. Let's run
17:44our code. How much would
17:46you like to deposit?
17:47Okay. A hundred
17:48dollars. Uh, let's bet on
17:49three lines. Let's try to
17:50bet $40 and says, you
17:52do not have enough to
17:52bet that amount. Your
17:54current balance is a
17:54hundred dollars. Okay.
17:55Uh, how much would you
17:56like to bet on each line?
17:57Let's go with $20. You're
17:59betting $20 on three
18:00lines. The bet is equal
18:01to $60. Okay. So we have
18:03now successfully got the
18:05number of lines, the
18:06betting amount, the
18:07deposit amount. Now,
18:08what we need to do is we
18:09need to actually run the
18:11slot machine. Now, this
18:12is where it gets a
18:13little bit more
18:13complicated and I'm going
18:14to start importing some
18:16modules. So the first
18:17module I'm going to
18:18import is the random
18:19module because we need to
18:21generate, um, the slot
18:22machine values kind of
18:24randomly, right? So how
18:25are we going to do this?
18:26Well, the first thing we
18:27need to figure out is how
18:29many items we want to
18:30have in each reel and how
18:32long we want the lines to
18:33be. Now slot machines can
18:36get a bit complicated in
18:37terms of how the lines
18:38work. I'm going to keep
18:40this really simple and
18:41we're going to imagine
18:41that we have a three by
18:42three slot machine and
18:44that you only get a line
18:45if you get three in a
18:46row. Okay. If you had
18:48three in a row, then you
18:48win. This might not be
18:49the most balanced slot
18:50machine. It might not be
18:51one you want to play on,
18:52but for this project is
18:53fine. So again, I'm going
18:55to set some values here
18:56that specify the number
18:58of rows and columns we're
18:59going to have in our slot
19:00machine. So I'm going to
19:02say Rose is equal to
19:03three and calls is equal
19:05to three. And if we want
19:06to make this a little bit
19:08better, we could say, I
19:09guess, actually, you know
19:11what row and call is fine
19:12for right now. I don't
19:12know exactly what they
19:13would call. Uh, like, I
19:15guess it'd be like real
19:16count and number of
19:17reels or something like
19:18that for now, though,
19:19this is fine. All right.
19:21Now what we need to
19:22specify is how many
19:23symbols are in each of
19:25our reels. Now it should
19:27be the same, at least
19:28from what I know, it
19:29should be the same
19:30number of symbols in
19:30every single reel. We're
19:31not doing anything
19:32really complicated. When
19:33I say real, I'm talking
19:34about kind of one
19:35column, right? So how
19:36many symbols are in that
19:37column? Because we're
19:38going to have to
19:38randomly select out of
19:40those symbols. And then
19:41we need values for our
19:42different symbols. So we
19:44need to pick kind of,
19:45first of all, how many
19:45symbols do we want to
19:46have in total? And what
19:48do we want those symbols
19:49to be? Uh, now to keep
19:50this easy, we can do
19:52something just like, you
19:53know, ABCD, like those
19:55are probably fine as the
19:56symbols. Um, yeah, we
19:58can do something like
19:59that. So let's say
20:00symbol underscore count
20:03is equal to, and let's
20:05make a dictionary here.
20:06Now for our dictionary,
20:08I'm going to have the
20:08symbol be a string, and
20:10I'm going to have the
20:11count of the symbol in
20:12each reel. So I guess
20:13what we can have is
20:14characters that are like
20:15at the beginning of the
20:16alphabet, like a, be the
20:17most valuable. So maybe
20:19we only have, I don't
20:20know, something like two
20:21A's in every single
20:22reel. And then for B's,
20:24we can have something
20:24like four of those for
20:26C's, we could have six
20:29and for D's we could
20:30have eight. Now, again,
20:32I don't think this is
20:33going to be very
20:33balanced slot machine.
20:34I'm not going for the
20:34best odds here. I'm just
20:35trying to kind of make
20:36something work. So let's
20:38see if this actually
20:38works for us. If every
20:39single reel, we have two
20:41A's, four B's six C's
20:43and, um, what do you
20:44call it? Eight D's to
20:45choose from. Now, the
20:47thing that I think is
20:48going to happen here is
20:48we're going to get a lot
20:50of situations where it's
20:51just D's that are being
20:52in the reels, but let's
20:54see if this works at all
20:55in terms of randomly
20:56selecting. Okay. So
20:57that's what we've done
20:58here. Now, what we need
20:59is something that's
21:00essentially going to
21:01generate what the
21:02outcome of the slot
21:03machine was using these
21:05values here. So to do
21:07this, I'm going to say
21:07define, and we'll say
21:09get underscore slot
21:12machine, underscore spin.
21:14Okay. And inside of
21:16here, what we're going
21:17to take is we're going
21:18to take the rose calls
21:21and symbols, and this
21:23will be the symbols
21:24that we pass. So these
21:25are three parameters
21:26that we're going to pass
21:26to this function. And
21:27then inside of here, we
21:28can use these
21:29parameters. So inside of
21:31this function, again,
21:32what we need to do is
21:33generate what symbols
21:34are going to be in each
21:35column based on the
21:37frequency of symbols
21:38that we have here. So we
21:39essentially need to
21:40randomly pick the number
21:42of rows inside of each
21:45column. So if we have
21:45three rows needs to pick
21:47three symbols that go
21:48inside of each of the
21:49columns that we have.
21:50And for each column,
21:51we're doing kind of a
21:51new random pick, right?
21:53A new random generation
21:54of the symbols. Now this
21:56can be a bit
21:56complicated. Now the
21:57easiest way to randomly
22:00select, um, values here
22:02for each of our columns
22:03is going to be to create
22:04a list that contains all
22:06of the different values
22:07we possibly could select
22:09from, and then to
22:09randomly choose three of
22:11those values. And when
22:13we choose a value, we'll
22:14remove it from the list
22:16and then we'll choose
22:16again. Now, what I'm
22:17going to do here is not
22:18going to be the most
22:19efficient algorithm, but
22:20since we're dealing with
22:20small values, this is
22:21fine. So let's see how
22:23we work with this. Okay.
22:25So what we're going to
22:26define here is all
22:28underscore symbols. Okay.
22:30Now this is going to be
22:31a list. And what we're
22:32going to do is write a
22:33four loop. That's going
22:34to add however many
22:36symbols we have here
22:37into the all symbols
22:38list. So I'm going to
22:40say four. And since
22:41we're iterating through
22:42a dictionary, I can do
22:45the following. I can say
22:46four symbol comma symbol
22:49underscore count. And
22:51then this is going to be
22:51in symbols dot items.
22:54Now, when you use dot
22:55items, what this does is
22:56give you both the key
22:58and the value associated
23:00with a dictionary. So I
23:01can get the key here and
23:03the value, and I can
23:04just use both of them
23:05rather than looping
23:05through the dictionary,
23:07only getting the keys
23:07and having to manually
23:09reference the values.
23:10Okay. So now that I have
23:11the symbol and the
23:12symbol count, I want to
23:13add this many symbols to
23:15the, um, what do you
23:16call it? Symbols list.
23:17Now there's a few
23:18different ways to go
23:19about doing this. I
23:20think the easiest way to
23:21do this is just going to
23:21be to run another four
23:22loop so that I don't
23:23confuse anyone. So we're
23:24going to say four, and
23:25then this is going to be,
23:27let's do this. I in
23:30range and then symbol
23:32underscore count. And
23:34actually we don't even
23:35need, I we're going to
23:36put underscore. Now this
23:37is an anonymous
23:38variable in Python. So
23:39whenever you need to say
23:40loop through something,
23:41but you don't actually
23:42care about the count or
23:43the iteration value, then
23:44you just put an
23:45underscore and then you
23:46don't have an unused
23:47variable anymore. So I'm
23:48going to say four
23:48underscore and range
23:49symbol count. I'm going
23:50to say all underscore
23:52symbols dot, and then
23:54this is going to be
23:54append and I'm going to
23:56append whatever the
23:57symbol is. So what's
23:58going to happen here is
23:59I'm going to loop
23:59through this dictionary.
24:00Let's imagine I'm on the
24:01first key value pair. My
24:03symbol is going to be a,
24:04and my symbol count is
24:05going to be two. All
24:06right. So then I have
24:07another four loop inside
24:08of here where I'm looping
24:10through the symbol count.
24:11So the symbol count is
24:12two. And what I'm doing
24:13is doing this two times.
24:14So I'm going to add this
24:15symbol twice into my all
24:17symbols list. All right.
24:19Now that we have the all
24:20symbols list, we need to
24:22select what values are
24:23going to go in every
24:24single column. So how do
24:26we do this? Well, let's
24:28make a four loop that is
24:29going to do this for
24:31every column. So I'm
24:32going to say my columns
24:34is equal to a list and
24:36inside of here, I'm
24:37going to place a bunch
24:38of lists, which are
24:39going to contain all of
24:40the values inside of my
24:41columns. Now, this may
24:42seem a little weird to
24:43any of you that have
24:44used a nested list
24:45before, because
24:46typically when you write
24:47a nested list, uh, you
24:48kind of have all of the
24:49interior lists here that
24:51are representing your
24:51rows. So if I had like
24:53zero, zero, then these,
24:55this would be the values
24:56that are in row zero,
24:57right? Or in the first
24:58row. And then this would
24:59be the second row and
25:00et cetera. However,
25:01here, we're doing it the
25:03other way around where
25:03each of these nested
25:04lists is going to
25:05represent the values in
25:06our call. So keep that
25:08in mind. I'll explain
25:09how this works in case
25:10any of you are a bit
25:11confused later on, but I
25:12just want you to know
25:13that we're storing the
25:13columns, not the rows
25:15inside of here. Okay. So
25:16we have all of the
25:17symbols and now for each
25:19of the columns that we
25:20have, uh, we need to
25:21generate, what is it?
25:23Uh, the values inside of
25:24the columns and how many
25:25values do we need to
25:26generate? Well, however
25:27many rows we have,
25:28that's how many values
25:29we need. So we're going
25:30to say four, and this is
25:31going to be call in
25:32range and then calls
25:34like that. And then we
25:36need to say four, and
25:38this will be row in
25:39range rows. Okay. So for
25:43every column, we need to
25:44generate a certain
25:45number of symbols. So
25:47inside of here, I'm
25:49going to say that my
25:50column is equal to an
25:52empty list. This is
25:53actually not going to
25:53have our columns inside
25:54of it. I'm just, was
25:55putting that there for
25:56an example. And now I
25:58need to select a certain
26:00number, number of values
26:01story from our all
26:02symbols list. So let's
26:04see how we do this. So
26:05we're going to say
26:07value is equal to random
26:10dot choice. And I'm
26:11going to choose from all
26:13symbols. Now I can use
26:15random because I
26:16imported random here.
26:18Now, what we're actually
26:19going to do though, is
26:20we're not going to use
26:20the all symbols list.
26:22We're going to use a copy
26:23of this and you'll see
26:24why, but what we need to
26:25do is once we pick a
26:26value, we need to remove
26:28it from this list so that
26:29we can't choose that
26:30value again, right? So
26:31if there's only two A's,
26:33we shouldn't be able to
26:34select three A's. We
26:35should only be able to
26:35select that most too. So
26:36if we select one, a, we
26:37need to remove it. So
26:38then the next selection
26:39doesn't have that as a
26:40chance. So that means we
26:42need to make a copy of
26:43this all symbols list,
26:44because if I start
26:45removing from this all
26:46symbols list, then when
26:47I try to do the next
26:48column, it's going to
26:49have values removed. So
26:51that's what we need to
26:51do. We need to make a
26:52copy. So I'm going to
26:52say current underscore
26:55symbols is equal to all
26:57underscore symbols. And
26:58the way you copy a list
27:00is you do this. You put
27:01a colon here. This
27:03operator here is
27:04referred to as the slice
27:05operator, because if I
27:06just did this and made
27:07it equal to all symbols,
27:09what happens is current
27:10symbols stores, the same
27:12object as all symbols.
27:14Now that means anything
27:15I do to all symbols
27:16affects current symbols
27:18and anything I do to
27:19current symbols affects
27:20all symbols. So this is
27:21not what we want. We
27:22don't want what's
27:22referred to as a
27:23reference. We want a
27:25copy. So the way you do
27:26the copy is you put a
27:27colon here, make sure
27:28you add this. Otherwise
27:29it's not going to work.
27:31Okay. Continuing here.
27:32Now, what we're going to
27:33do is select from our
27:34current symbols, and
27:36then we're going to
27:37remove whatever this
27:38value is from our
27:39current symbols list.
27:40So we're going to say
27:41current underscore
27:41symbols dot remove, and
27:44then value. Now, when
27:45you do dot remove, it's
27:46just going to find the
27:46first instance of this
27:47value in the list and
27:49get rid of it. Okay. Now
27:51that we've done that, we
27:52want to add this value
27:54to our column. So we're
27:56going to say column dot
27:59push, and sorry, not
28:01push non JavaScript
28:02right now. I'm in
28:03Python dot append the
28:05value. All right. And I
28:06just realized here, we
28:07don't actually need the
28:08column and we don't need
28:10the row. So we can just
28:11put underscores there.
28:13Now, what we're going to
28:13do after this is we're
28:14going to say columns dot
28:16append, and we are going
28:17to append our current
28:19column. Now let me run
28:20through this. Cause I
28:21understand it's a little
28:21bit confusing. We start
28:23by defining our columns
28:24list. Then we are going
28:26to generate a column for
28:28every single column that
28:29we have. So if we have
28:29three columns, we need to
28:30do everything inside of
28:32here three times. That's
28:33why we have this first
28:33four. Then inside of
28:35here, what all this code
28:36is doing is it's picking
28:37random values for each, I
28:39guess, row in our column,
28:41right? For each row
28:42each value that we're
28:42going to have. So we say
28:44a column is equal to an
28:45empty list. We say our
28:46current symbols, which
28:47are the ones we can
28:47currently select from is
28:49equal to a copy of all
28:50symbols. Then we loop
28:52through the number of
28:53values that we need to
28:54generate, which is equal
28:55to the number of rows
28:56that we have in our slot
28:57machine. Then we say it,
28:58the first value we're
28:59going to get here, or a
29:00value we're picking is
29:02random, not choice
29:03current symbols. This
29:04picks a random value from
29:06this list. Okay. We then
29:08say current symbols dot
29:09remove the value. So we
29:10don't pick it again. And
29:11then we add the value to
29:12our column. Okay. Once
29:14all of that's done. So
29:15this four loop is
29:15finished. We now should
29:16have however many rows
29:18there are symbols inside
29:19of our column. We now add
29:21our column to our
29:22columns list. Then
29:23finally we can go here
29:26and we can return our
29:28columns. Okay. And
29:29remember that when we're
29:30looking at this list
29:31here, every interior list
29:33gives us the value of the
29:35items inside of our
29:36call. I know a little bit
29:37confusing. This was quite
29:39a bit of logic,
29:39especially if you're a
29:40beginner programmer, but
29:41I told you I wanted to
29:42make this challenging
29:43enough to be interesting
29:44for you and show you some
29:45new stuff. So don't worry
29:46if this doesn't make
29:47complete entire sense,
29:49maybe read through this a
29:50few times, pause the
29:50video, go back and listen
29:52to the explanation again.
29:53Uh, but this is, you
29:55know, how we generate the
29:56items that are going to
29:57be in our slot machine.
29:59Now that we have this, we
30:00want a way to print this
30:01out. We want to look at
30:02this because I can't even
30:03really test this yet
30:04until I can print out
30:05what's inside of all of
30:06my columns. And I want to
30:07print this in like a nice
30:08way. So I'm going to make
30:10a function here and say,
30:11define print underscore
30:14slot machine. And what
30:15we're going to take here
30:16is our columns. Now, when
30:19we have our columns, it's
30:21not really in a format.
30:22That's easy for us to
30:23print because we have all
30:25of our columns kind of
30:26laid out as rows almost,
30:28right? Like we have
30:29maybe actually this isn't
30:30going to be good, but we
30:31would have like a B C and
30:33one, maybe we have like a,
30:35a, a, and again, these
30:37are our columns, not our
30:39rows. So what I need to
30:40do is kind of flip these
30:41around. So rather than
30:42having like a B C a, it
30:45would go a B C right in
30:49the first column. And
30:50then this would say a, a,
30:53and then a like that. I
30:54know that I kind of
30:55butchered this example,
30:56the way that I wrote it
30:56out, we need to change it
30:58from being this way to be
30:59this way, right? That's
31:00how we need to print it
31:01out. So how are we going
31:02to do that? Well, this
31:04operation is actually
31:05referred to as trends
31:06posing because we have
31:07what's known as a matrix.
31:09We don't need to get
31:09into all the fancy words,
31:11but understand this is
31:12known as trends posing.
31:13So the way we're going to
31:14do this is we're going to
31:15write a four loop and
31:16we're going to say four.
31:17And then this is going to
31:18be row in range. And then
31:23we need to determine the
31:23number of rows that we
31:24have based on our
31:25columns. Now, the number
31:27of rows that we have is
31:28the number of elements in
31:29each of our columns,
31:30right? Cause that's the
31:31number of vertical spaces
31:32we have. So we need to
31:33look at a column and get
31:34the length of that. So
31:35we're going to say the
31:36length of columns zero.
31:39Now this assumes that we
31:40have at least one column
31:42and we should always
31:43have one column. So it's
31:43fine to put this here,
31:45but understand that if
31:45we did pass something
31:46that had no columns,
31:47this would crash because
31:48there'd be no column at
31:49index zero to access. So
31:51I'm saying four row in
31:52range, the line of
31:53columns, zero. Then what
31:56I'm going to do is I'm
31:57going to loop through all
31:58of my columns and only
31:59print the first value in
32:01it, or whatever the
32:01index of my current row
32:03is. Again, I'll explain
32:04this in a second. I know
32:05this is a bit confusing.
32:06So I'm going to say four,
32:08and this is going to be
32:08column in and then
32:10columns like that. And
32:12when I do this, now I'm
32:14looping through all of
32:15the items inside of
32:16columns. So it's giving
32:17me every individual
32:18column. So now that I
32:20have a column, I'm just
32:21going to print the value
32:23that's at the first row
32:25of that column. So I'm
32:26going to say print, and
32:28then I'm going to print
32:29column at row. And I'm
32:31just going to put a
32:32comma here and put a
32:33pipe operator. And the
32:35reason I'm going to put
32:35a pipe operator is so
32:37that we have some
32:37separation between the
32:39different items, right?
32:40Now, I just want to make
32:41sure though, that I only
32:42put this pipe operator
32:43here. If we're not
32:45printing the last
32:46column, because if
32:47we're printing the last
32:48column, then we don't
32:49want to have the pipe
32:50like off, right? We only
32:51want to have two pipes
32:52in the middle, not one
32:54at the very end. So the
32:55way I need to check
32:56this, I need to say four
32:57I comma column in
32:59enumerate columns. Now
33:01when you enumerate, what
33:02this does is give you
33:03the index. So 0 1, 2, 3,
33:06as you loop through as
33:07well as the item. So now
33:09that I have, I, what I
33:10can actually do here is
33:13I can say, uh, there's a
33:15few ways to do this.
33:16Let's actually go back
33:17to this. We'll say if I
33:19does not equal, and then
33:20this is going to be the
33:21Len of columns minus
33:24one, we'll do this.
33:27Otherwise we'll print the
33:28same thing. We just won't
33:29print the pipe. Now
33:31there's a bunch of
33:32different ways we could
33:32have done this, but this
33:33is just the way that I
33:34think is going to be
33:34easiest. So that's how
33:35we'll do it. So the
33:36reason I'm checking, if
33:37I is not equal to Len of
33:39columns minus one is
33:40because the Len of
33:41columns minus one is the
33:42maximum index we have to
33:44access an element in the
33:45columns list, right? If
33:47we have a columns list
33:48with three items, the
33:49length is three. The
33:50maximum index is two. So
33:52if I is not equal to the
33:53maximum index, print the
33:54pipe, otherwise don't
33:55print the pipe. That's
33:56how it's going to work.
33:57Okay. So now we have
33:58print slot machine and
33:59get slot machine spin.
34:00That's all. This is all
34:01we need for printing the
34:02slot machine. So we loop
34:03through every single row
34:05that we have for every
34:07single row. We loop
34:08through every column and
34:10for every column, we only
34:11print the current row
34:12that we're on. So we're
34:13going to print, um, what
34:15is it? Row zero. So all
34:16of the elements in row
34:17zero first, then row one,
34:19then row two. And this
34:20essentially transposes or
34:22flips our columns from
34:23being this way to be
34:24vertical this way. All
34:26right. So let's try this
34:26down and then we'll kind
34:28of finish the program
34:29because we're actually
34:29getting quite close to
34:31all right. So once we
34:32determine what they're
34:33betting down here, what
34:35we need to do is
34:36generate the slot
34:36machine. So we're going
34:37to say slot. We'll say
34:39just slots is equal to,
34:41and then this is, what
34:43did we call this
34:43function? We called it
34:45get slot machine spin.
34:47Okay. So get underscore
34:50slot machine spin, and
34:51then auto-filled this
34:52formula. We're passing
34:53the rows, columns, and
34:54symbols. So rose is all
34:56capitals. Actually calls
34:58is like this. And the
35:00symbols, I believe we
35:01called this the symbols
35:02count, uh, or we call it
35:03symbol count. So let's
35:04go here and make that
35:06symbol count. Okay. So
35:08now we should have all
35:09of the columns in our
35:10slot spin, right? That's
35:11what that did for us.
35:13Now, what we need to do
35:14is we need to print this.
35:15So we're going to say
35:16print slot machine, and
35:18we're going to pass to
35:18this, our slots. And
35:20really this is the
35:21columns, but I'm just
35:22calling them slots
35:23because each one is like
35:24what's in the slot,
35:25right? Okay. Let's give
35:27this a shot guys. There
35:28probably will be an
35:29error because that's
35:30usually what happens
35:31when you write this
35:31much code from scratch,
35:32but let's see if this
35:33works. Okay. So we are
35:35depositing. Let's
35:36deposit a hundred
35:36dollars and to the
35:38number of lines to bet
35:39on, uh, let's bet on
35:41two. And then how much
35:43would you like to bet on
35:44each line? Let's do $10.
35:45Okay. Now already we got
35:47a bit of an error here
35:48because I forgot to do
35:49something, which I'll
35:50add in a second. But if
35:51I scroll here, you can
35:53see that what's
35:53happening is we're
35:54printing everything
35:55correctly. We're just
35:56not printing it, um, on
35:58the same line, which is
35:59what we need to do. So
36:01ideally we want D a and
36:02D on the same line, CB
36:04and C on the same line
36:05and BC and a on the same
36:06line. So the way that we
36:07fix that, my apologies
36:08here, guys, is we go
36:11back to print slot
36:12machine. And here we
36:14add this thing called
36:15end equals, and we just
36:16make this a, um, what
36:18do you call an empty
36:19string? And in fact,
36:21let's actually do this
36:23and is equal to a pipe
36:26with spaces. All right.
36:27So what end does is it
36:28tells the print
36:30statement, what to end
36:31the line with now by
36:32default end is equal to
36:34what's known as the new
36:35line character or the
36:37return character or
36:38whatever you want to
36:39call this carriage
36:40return. There's
36:41different names for it.
36:42I call it the new line
36:43character. Now,
36:43backslash. And if you
36:44ever print this out,
36:45it's what tells the, um,
36:47console to go to the
36:48next line. So we don't
36:49want to print that
36:50because if we print that
36:51at the end, then that
36:52means that we move to
36:53the next line after
36:54every single row. We
36:55only want to do this.
36:56Sorry, after every
36:57single column, we only
36:58want to do this after
36:59every single row. So
37:00I'm going to change this
37:00to be a pipe. So now
37:02we'll just print this
37:03pipe at the end of our
37:04print statement. So
37:05after we print this
37:06here, we're just not
37:07going to print
37:07anything. And now we
37:09need one more check to
37:10see if we should go to
37:12the next line or not,
37:13because every row we
37:14want to go down to the
37:15next line. So I'm
37:16actually going to say
37:17here, uh, print, and we
37:20can just do an empty
37:21print statement. Now,
37:22by doing an empty print
37:23statement, it just
37:24brings us down to the
37:25next line because it
37:26prints a new line
37:27character by default at
37:28the end of the empty
37:29print statement. It
37:30seems a bit weird.
37:31That's kind of how this
37:32works. We're going to
37:32print the first row.
37:34This will go on the
37:35same line. Then we're
37:36going to print a new
37:37line character. So it
37:38brings us down to the
37:39next line, print the
37:40next row, new line
37:41character, next row,
37:42new line character,
37:42next row, et cetera.
37:44Right? So let's have a
37:45look at this now. Okay,
37:46let's go here. Let's
37:48run. How much would
37:49you like to deposit a
37:50hundred dollars? Uh,
37:51let's bet on two lines.
37:52How much would you like
37:53to bet $10? And there
37:55we go. Now we have our
37:56slot machine, excuse
37:57me. And this is
37:58correct, right? So we
37:59have all of our columns,
38:01uh, and then all of our
38:02rows. And what we'd now
38:03be checking is if we
38:05have three in a row,
38:07right? That's what we're
38:07going to have to check.
38:09Okay. So we have our
38:10slot machine. We've
38:11spun it. We probably
38:12should have some input
38:13that asks, like, do you
38:14want to spin the slot
38:15machine? And then they
38:15can hit spin, blah,
38:17blah, blah, be a bit
38:17more interactive, but
38:19that's how it works. And
38:20just to be clear here,
38:20we're going to check if
38:21we have three in a row.
38:23And if we do based on
38:25the value of the symbol,
38:26we're going to multiply
38:27their bet and then give
38:28them that amount. All
38:29right. So let's write a
38:30function that can do
38:31this. Now, when we're
38:32checking if they want or
38:33not, we need to know
38:35what their bet is, which
38:37lines they bet on. Right.
38:39And then we can actually
38:40check now the way the
38:42lines works again. I
38:44know this is not exactly
38:45how like a real casino
38:46slot machine will work.
38:47We're going to make it
38:47so that if you bet on
38:48one line, you just bet
38:49on the top line. If you
38:51bet on two lines, you
38:51bet on the top in the
38:52middle. If you bet on
38:53three lines, you bet on
38:54all three. I'm not going
38:55to let the user pick
38:55like where the one line
38:57is they want to bet on.
38:58We'll just say one is
38:58top two is both the top
39:01two, three is all of
39:02them. And that's how it
39:03will work. Okay. So
39:04let's do this. Let's go
39:06define check winnings.
39:09Okay. Now to do this, we
39:11need the slot machine
39:12itself. We need the
39:14lines. So we're going to
39:15say lines and we need
39:16the bet. Okay. So how
39:18are we going to do this?
39:19Well, we just need to
39:20look at the rows that
39:22the user bet on. So
39:23let's start with that.
39:24And then we can check
39:25each row and determine
39:26a value also for the
39:27symbol. So we're
39:28actually going to take
39:29one more thing. I'm
39:30going to say values.
39:31Now I'm also going to
39:31make the values here.
39:32So I'm going to say
39:33symbol underscore
39:34value. And I don't know
39:36what actually makes
39:37sense again to do for
39:38this, but we're going to
39:39say that D is going to
39:41be a two times
39:41multiplier. C is going
39:43to be a three times
39:45multiplier. This will be
39:46four, and then this
39:47will be five. So, you
39:49know, the more rare the
39:49symbol is, the higher
39:50your Beckett's
39:51multiplied. Again, I
39:52don't think this is a
39:53fair slot machine, but
39:53that's fine. Okay. So
39:55check winnings. Now we
39:56have columns, lines,
39:57bet, and value. Now
39:58let's look through only
39:59the rows that they bet
40:00on. So the lines that
40:01they bet on. So to do
40:03this, we're going to
40:03say four, uh, and we'll
40:05say line in range. Then
40:09this is going to be
40:09lines like this. Now,
40:12the reason this will
40:13work is because if they
40:14bet on one line, then
40:16we're going to go up to
40:17one line, but not
40:18include it. So that
40:18means the line will be
40:19equal to zero, right? So
40:21if they bet one line,
40:23then this is one. And
40:24that means this is only
40:25ever going to be zero.
40:26If they bet on two
40:27lines, then line will go
40:28to zero and one. So
40:29we'll check the zero
40:31with row, which is line
40:31one. And the first row,
40:33which is line two, if
40:34they bet on all three
40:35lines, then line will be
40:36zero, then one, then
40:38two. So we'll check all
40:39three of those rows,
40:40right? And this will
40:41work dynamically, even
40:41if there was more lines
40:43to bet on. So inside
40:44this four loop, we need
40:45to check that every
40:46single symbol in the
40:47line or row that we're
40:49checking is the same. So
40:50we can get the first
40:52symbol that's in this
40:53row, and then just make
40:54sure it's the same for
40:55the rest of the symbols.
40:56So to do that, we're
40:57going to say symbol is
40:58equal to columns zero at
41:00the current line. Now,
41:01the reason we're using
41:02columns zero is because
41:05we have all of the
41:06columns, not all of the
41:07rows. It makes it a bit
41:08more complicated. So we
41:09need to look at the
41:10first column because
41:11that's where the first
41:12symbol is always going
41:13to be for each row and
41:14then get whatever line
41:15we're on. So first
41:17column and then either,
41:18you know, line zero,
41:19line one, line two,
41:20et cetera, going down
41:21that first column gives
41:22us the first symbol. And
41:23we're going to assume
41:24that we always have at
41:25least, you know, one
41:26real, right? So at least
41:27one call. So now that we
41:28have the symbol, we're
41:29going to loop through
41:29all of our columns. So
41:30we're going to say four
41:32column in columns like
41:35that. Now, what we want
41:37to check. So the symbol
41:39to underscore check is
41:41equal to the column at
41:43whatever row we're
41:44looking at. So if we're
41:45looking at row zero,
41:46we're checking the first
41:47symbol in the column for
41:48looking at row one,
41:49checking the second
41:50symbol in the column,
41:51et cetera, similar check.
41:53We're then going to say
41:54if the symbol is equal
41:58to the symbol to check
41:59and actually we'll say
42:00if it is not equal to
42:02the symbol to check,
42:03then we are going to
42:05break. Okay. And the
42:06reason we're breaking is
42:07because if we found one
42:09of the symbols is not
42:11equal to the previous
42:12symbol, uh, or equal to
42:13all of the symbols that
42:14should be in this, um,
42:16row, then we just
42:17break out of the four
42:18loop. And what we'll do
42:19is we'll put an else
42:20statement here. And what
42:22this else statement will
42:23do is it will tell us if
42:24we didn't break out of
42:25the four loop. So I'm
42:26sure you probably haven't
42:27seen this before, but you
42:28can do a four else where
42:31if you break this L
42:32statement does not run,
42:33but if no break occurs
42:34within the four loop,
42:36then this L statement
42:38executes. So in the L
42:39statement here, what
42:40we'll do is we'll figure
42:42out how much they want.
42:42So we'll say winnings is
42:44equal to zero. Okay. And
42:46then here, we're going to
42:47say winnings plus equals.
42:50And then this is going to
42:51be the values at the
42:54symbol multiplied by the
42:57bet. And then we can go
42:58here and return the
43:01winnings. Okay. So let's
43:03look at this because I'm
43:04sure this is confusing.
43:05We have every line in the
43:07lines, which means we're
43:08looping through every row,
43:09essentially that we're
43:10going to be checking the
43:11user bet on. We then say
43:12the symbol that we want
43:13to check is whatever
43:15symbol is in the first
43:17column of the current row,
43:18because all of the
43:19symbols need to be the
43:20same, right? We then say
43:22four column in column. So
43:23we now know the symbol
43:24that we're going to
43:24check. Now we need to
43:25loop through every
43:26single column and check
43:27for that symbol. So we go
43:29to each column and we say
43:31the symbol to check is
43:32equal to the column at
43:34the current row that we
43:35are looking at. So if
43:36it's row zero, we're
43:37doing row zero. If it's
43:37row one, row one, et
43:38cetera, we then check if
43:40these symbols are not the
43:41same. If they are not the
43:43same, we break out, which
43:44means that we're going to
43:45go check the next line
43:46because they didn't win
43:47because symbols were not
43:48the same. If they are the
43:50same, then we don't break.
43:51And if we get to the end
43:52of this for loop and
43:53we've not broke out,
43:54which means all of the
43:55symbols were the same,
43:56then that means that the
43:57user won. And what they
43:58won is whatever the
44:00multiplier is for that
44:01symbol times their bet.
44:03Right. And this bet is
44:05the bet on each line, not
44:07the total bet, right? So
44:09they could win on one
44:09line, but lose on the
44:10other line. That's how it
44:11works. Okay. Hopefully
44:13that's clear. Uh, I can't
44:14explain it much more than
44:16that. Just understand
44:17symbol values here,
44:18right? Uh, it gives us,
44:20you know, five, four,
44:21three, two. So that's
44:22what I'm using. I'm
44:22saying values at
44:23whatever the symbol is
44:24multiplied by the bet.
44:25And that's how we're
44:26adding to the wings.
44:27Okay. So that's it for
44:28determining how much
44:29they want. So let's run
44:31the game one full time
44:32now. Uh, and then we'll
44:33figure out how we can run
44:34it multiple times and
44:35reduce the balance and
44:36all of that, which is
44:37easier than what we just
44:38did. Okay. So we print
44:39the slot machine and now
44:40we're going to say
44:42winnings is equal to,
44:44and what did I call this?
44:46Uh, check winnings. Okay.
44:50Check winnings. We need
44:51to pass in this, our
44:52slots, the number of
44:56lines. So I think we
44:57called that lines,
44:58right? The bet, which we
45:00have right here and the
45:03values, which is called
45:05symbol value. So let's
45:06go symbol underscore
45:07value. All right. Then
45:09we're going to print
45:13you one and then
45:15whatever the amount
45:16is that they want. So
45:17let's make this an F
45:17string. Okay. And then
45:20you won. And then this
45:21is going to be
45:23winnings like this. And
45:27this could potentially
45:28be $0, right? Okay. So I
45:30think that's good. I'm
45:32thinking that we might
45:33want to also tell them
45:34how many lines they want
45:35on or what lines they
45:36want on. So let's
45:37actually return that as
45:39well from this function.
45:41So get, uh, check
45:42winnings. We're going to
45:43do one more thing here.
45:44We're going to say
45:44winning underscore
45:46lines is equal to an
45:47empty list. And if they
45:49win, we're going to say
45:50winning lines plus
45:52equals, uh, I should
45:54not plus equals dot
45:55append. And then this is
45:56going to be whatever the
45:58current line number is,
45:59but we're going to have
46:00to add one to it because
46:01this is an index and we
46:02want like line one, two,
46:03three, not zero one,
46:04two. So we say winning
46:06lines, not a pen line
46:07plus one. Then we go
46:08here and we put winning
46:09lines. And now we're
46:10returning two values, the
46:11total amount they won as
46:12well as what lines they
46:13won on. Okay. Now let's
46:15go here to wing lines
46:17and we're going to say
46:18winnings and winning
46:20underscore lines. You
46:22won these winnings. And
46:24then we'll say you won
46:26we'll print another line
46:29like this F you won on.
46:33And then this is going to
46:34seem a little bit weird,
46:35but I'm going to do
46:35asterisks. And then I'm
46:36going to pass my winning
46:38lines. Now, again, I
46:39promised I was going to
46:40show you guys some new
46:40stuff. So I'm, I'm
46:41showing you some
46:42advanced syntax here,
46:43but what this does is
46:45it's actually called the
46:45splat operator or the
46:46unpack operator. And
46:48it's going to pass every
46:49single line from, uh,
46:51this winning lines list
46:54to, uh, this print
46:56function. So what that
46:57means is if we have
46:58lines like one and two,
46:59it's going to pass both
47:00one and two. So it's
47:01going to say you won on
47:02and then one, two. So
47:03I'm going to say you
47:04want on lines like this.
47:06And then it would say
47:07one space, two, one on
47:08all three lines, say one
47:09space, two space, three.
47:10If we didn't want to any
47:11lines, then it's not
47:12going to say anything.
47:14Okay. You'll, you'll see
47:15when, when we print this
47:15out, how this works. So
47:17let's run this here.
47:19Let's make this full
47:19screen now. And let's go
47:20through an example. So
47:21deposit a hundred
47:22dollars. Uh, how many
47:23lines for invalid? Okay.
47:25Let's bet on two lines.
47:26How much would you like
47:27to bet on each line? $10.
47:29All right. So we won $0
47:31because none of the
47:31symbols are the same. So
47:32let's try again. All
47:33right. Posit a hundred
47:34dollars, uh, two, uh,
47:37two lines. Sorry. How
47:38much would you like to
47:38bet? $10. You won $0.
47:41Okay. So this is going to
47:43happen a few times. So
47:44rather than continuing to
47:45do this and hoping that
47:46we're going to win, let's
47:48make it so that we can
47:48run this multiple times.
47:49So they're having to
47:50constantly rerun the
47:51program. So we have this
47:53main function. We have
47:54the balance and we have
47:55the lines. Now the
47:56balance is going to stay
47:57the same, but all of this
47:58stuff here needs to
47:59happen multiple times.
48:01Right. And we'll reduce
48:02from the balance and all
48:02of that stuff. So let's
48:05copy all of this into a
48:08function and let's say
48:09define, let's say game.
48:11Okay. We'll put this
48:13inside of here. So this
48:14kind of executes one
48:16game. Now what we can do
48:18is write a while loop
48:19here in this while loop
48:22can kind of handle
48:23running the game. All
48:24right. So I'm just
48:24thinking here, if we do
48:26this, we need some way to
48:27determine like from this
48:28instance of the games,
48:30like per spin. So
48:31actually let's just say
48:32this is maybe spin. We
48:33need to know how much
48:34they won or they lost.
48:35So I'm just going to
48:36return here, the
48:37winnings minus the total
48:39bet. And that will tell
48:40us how much they won or
48:41lost from this spin,
48:44because if they won a
48:44hundred dollars, but
48:45they bet 15, then they
48:46only won 85. So then
48:48here we would update
48:49and we would add 85,
48:51right? But if they
48:52didn't win anything,
48:52then it's going to be
48:53negative, whatever the
48:54total bet is. So like
48:5515 bucks. So we'd
48:56subtract 15 from the
48:57balance. Okay. So here
48:58I'm going to say, well,
48:58true. And we'll print,
49:02um, current balance is,
49:06and then we can just put
49:07comma, actually let's do
49:09this as an F string. So
49:10we can put a dollar
49:10sign current balance is,
49:13and then we'll do
49:13dollar sign and then
49:15balance. Okay. And then
49:17we'll play games. We'll
49:18say spin equals input,
49:23say, press, enter to
49:28spin. Okay. So they'll
49:30press enter to spin. So
49:31actually we don't even
49:31need to put a value here
49:33because we don't care
49:33what they type in, or
49:35actually we can say this
49:36spin is equal to press
49:38enter to spin. And we'll
49:39say Q to quit. So if
49:42they type in Q, then
49:43we'll quit. So we can
49:43say, if spin is equal to
49:48Q, then we will break
49:50the wall loop, which
49:50will just end the game.
49:52Okay. Otherwise we'll
49:53spin. So we can say spin
49:55like that. And we'll say
49:57balance plus equals
49:59spin. So the way that
50:00this works now is that
50:01spin is going to return
50:02to us. However much they
50:03won or lost, it will
50:05tell them like what they
50:06want or lost. So then
50:07we'll just update the
50:08balance based on the
50:08result of that spin. And
50:10then we'll run this
50:10again and say, okay,
50:11current balance is this
50:12press enter to spin,
50:13blah, blah, blah. And
50:14then when they spin,
50:14they're going to have to
50:16enter this info and
50:16actually press enter to
50:18spin. Probably doesn't
50:20make sense to go here,
50:21right? Because you only
50:22want to spin after you
50:24enter your information.
50:25Although we can just
50:27have this work, we'll
50:28say, press enter to play.
50:30That's fine. And then
50:30they can do it inside of
50:31there. Uh, okay. Now
50:33that we have that, I
50:34think that's actually all
50:36that we need. I'll just
50:37make one last print
50:38statement here that
50:39says you left with, and
50:44then we'll put it inside
50:45dollars and then this
50:46will be the balance and
50:48we'll put an F string
50:48here. All right, let's
50:51run this and give it a
50:51shot. Okay. So run, go
50:54here. How much do you
50:55like to deposit? Let's
50:56deposit a thousand
50:57dollars. Okay. Current
50:58balance is a thousand
50:59dollars. Press enter to
51:00play cue to quit. Okay.
51:01Enter. Uh, and we got an
51:03object here. Balance plus
51:04equals spin. All right.
51:06It looks like we need to
51:06fix this. So balance is
51:07equal to deposit balance
51:09plus equals spin. We have
51:11winning lines subtracted
51:12by total bet. And what is
51:15the problem? All right.
51:16So the issue here is that
51:17I accidentally have the
51:18name, my function spin
51:21the same name as this
51:22right here. So we're just
51:23going to change this to
51:24be answer. So now this
51:27will work properly. Okay.
51:28So my apologies guys,
51:29let's go here and fix
51:31this. And let's say
51:32thousand dollars. Current
51:34balance is a thousand
51:35percent or play. Okay.
51:36And to the number of
51:36lines to bet on, it's
51:37bet on two lines. It's
51:38about 10 bucks. Okay.
51:40Uh, balance is not
51:41defined. It looks like we
51:43need to fix another
51:44error. So what we need to
51:44pass here is balanced to
51:47the spin function so that
51:48it can actually check
51:49this when we make a bet.
51:50So of course that makes
51:51sense. Just forgot to do
51:52that. So let's pass
51:54balance here and now it
51:55will actually be able to
51:56check that. All right. So
51:57let's try it again. Third
51:58time's a charm, a
52:00hundred dollars. Current
52:01balance is a hundred
52:02dollars. Press enter to
52:03play. Okay. Number of
52:04lines. Let's go three.
52:05Let's go $10 and you are
52:07betting on $10. You're
52:10betting $10 on three
52:11lines. Total beds equal
52:11to $30. Okay. You won
52:13zero dollars. You won on
52:14lines and it has an issue
52:16here on sport operands
52:17for list and int. Okay.
52:20My apologies. That's
52:21because I put winning
52:22lines. This needs to be
52:24winnings. All right.
52:26Fourth time's a charm. So
52:27let's make sure our
52:28variable names are
52:28correct. That was just a
52:29silly mistake. Let's
52:30clear and run a hundred
52:33dollars. Enter three
52:35lines, $10. Okay. You
52:38won $0. You won on lines.
52:39Your current balance is
52:41$70. Okay. Let's play
52:42again. Three lines, $5.
52:45Okay. Did we win
52:46anything? No, we did not.
52:47All right. Let's play
52:48again. I'd really like to
52:49try to win something, but
52:51as we can see, the slot
52:52machine is not in our
52:52favor today. $10. You won
52:55$0. Okay. Current balance
52:57is $25. All right. Three
52:58lines, $2. You won $4.
53:01Okay. Nice. So how
53:03did we end up winning a
53:05$4? We bet $2 and we got
53:07a line here of DS. So
53:09that gave us a times two
53:11multipliers. So two
53:11times two, four. So we
53:12won four bucks. There you
53:13go. And then our balance
53:14get up, gets updated,
53:15sorry, to $23 because we
53:18were at 25, uh,
53:20subtracted six at four.
53:21Yes. Okay. So that makes
53:22sense. All right. So
53:23let's play again. Let's
53:24go three lines, $1. Okay.
53:26You won nothing. Let's
53:28play again. Three lines,
53:29$1. You want nothing?
53:30Three lines, $1. Uh,
53:33oops. Uh, this is, let's
53:35go $3. You want nothing?
53:36Okay. Play again. One
53:38line, $3. You want
53:40nothing? All right.
53:41Well, I won't continue to
53:42play, but you get the
53:43point. We've just created
53:45a slot machine. And now
53:45if I hit Q and enter, we
53:47leave with $11. All
53:50right. So I think I'm
53:52going to wrap it up
53:52there. I'm going to zoom
53:53out a bit. So you guys
53:54can read more of this
53:55code. I'll just kind of
53:56scroll through it. Also
53:57put it in the
53:57description in case you
53:59want to reference it on
53:59GitHub, but you can see
54:01this is what we wrote.
54:02So we did about 150
54:04lines of code. And I
54:05guess about now, now I
54:06walked you through my
54:07thought process. I
54:08showed you a bunch of
54:09different Python
54:09features. We did
54:11actually some fairly
54:11advanced logic in terms
54:13of looking through rows
54:14and columns and looking
54:15at nested lists. I
54:16showed you about
54:17functions. I showed you
54:18about the anonymous
54:19variable. You saw this,
54:21uh, splat operator here.
54:23And when we won on a
54:24line, you saw it said we
54:25won on whatever the line
54:26number was. So
54:28hopefully this was
54:29helpful to you guys.
54:30Hopefully this showed
54:30you kind of how to
54:31structure a program.
54:33Notice that we put
54:34everything in separate
54:34functions. We've kind of
54:35spread our code out.
54:36We've made it very
54:37readable. And now we
54:38know if there's a bug
54:39anywhere, it's really
54:39easy for us to go and
54:40kind of figure out where
54:41that is because we've
54:42separated everything
54:43into different units,
54:44right? Different blocks
54:45of code. Anyways, I will
54:46wrap it up here. I hope
54:47you guys enjoyed this
54:48video. If you like this
54:49style of teaching, then
54:50make sure you check out
54:51programming expert.io.
54:52And I look forward to
54:53seeing you in another
54:54YouTube video.
54:59Transcribed by https://otter.ai