Coding

For Loops & While Loops in Python – Beginner Python Tutorial #5 (with Exercises)

  • 00:00:00 what's up guys and welcome back to the
  • 00:00:01 fifth episode of my Python tutorial
  • 00:00:03 series in this video we are gonna go
  • 00:00:05 over four and while loops which are two
  • 00:00:09 different ways to repeatedly execute
  • 00:00:11 blocks of code also I want to mention
  • 00:00:13 that once you finish this video you'll
  • 00:00:15 have enough skills to build a basic game
  • 00:00:17 in Python so make sure you look in the
  • 00:00:20 description for a link on how to build
  • 00:00:22 this game that I'll show you right now
  • 00:00:25 basically it's a simple game where
  • 00:00:27 you're controlling the red block by
  • 00:00:29 moving the left and right arrow keys and
  • 00:00:31 these blue blocks are falling and you're
  • 00:00:33 trying to dodge them and basically the
  • 00:00:37 score goes up each time you've
  • 00:00:38 successfully dodged a blue block it gets
  • 00:00:40 to the bottom and as the score increases
  • 00:00:43 the blue blocks start falling faster and
  • 00:00:46 faster
  • 00:00:55 and if I ever do collide with a blue
  • 00:00:57 block it says down here game over your
  • 00:01:01 score so this is the game that we will
  • 00:01:03 be able to build once you finish all the
  • 00:01:05 things that would cover in this video
  • 00:01:06 all right let's get started all right
  • 00:01:09 let's begin with the for loop so let's
  • 00:01:11 say I wanted to print out the words
  • 00:01:12 hello world ten times but I didn't want
  • 00:01:15 to just type out you know print hello
  • 00:01:18 world and copy and paste that ten times
  • 00:01:20 just like that's kind of annoying so we
  • 00:01:23 can use a for loop to simply to simplify
  • 00:01:25 this for us so what I can do instead of
  • 00:01:27 just copy and pasting is I can do for I
  • 00:01:30 in range 10 and then make sure you end
  • 00:01:36 this forced it for loop with a colon and
  • 00:01:40 then I can just do print hello world and
  • 00:01:46 I run that and as you can see down below
  • 00:01:50 it printed out hello world ten times
  • 00:01:52 just like I wanted all right so that's
  • 00:01:55 just a basic example and I can change
  • 00:01:57 this to whatever value I want so range
  • 00:01:59 is how many times you want it to loop
  • 00:02:01 through so if I did it a hundred times
  • 00:02:03 you see that I have a hundred dollar
  • 00:02:06 where else now it does it really really
  • 00:02:08 quickly it's worth noting that in this
  • 00:02:11 statement so if I go back to range ten
  • 00:02:14 so in the statement this I variable
  • 00:02:17 right here represents what iteration
  • 00:02:19 we're on so I could do something like
  • 00:02:22 print I instead and if I did for I in
  • 00:02:25 range 10 and then I printed I it's going
  • 00:02:27 to print out the numbers 0 through 9 and
  • 00:02:31 it starts at 0
  • 00:02:32 the main reason that starts at 0 is just
  • 00:02:35 like indexing we count we count starting
  • 00:02:37 from 0 in Python but it does include 10
  • 00:02:41 iteration so 0 through 9 is 10 total 10
  • 00:02:45 right here is the first number that we
  • 00:02:46 don't include so we can print out I and
  • 00:02:49 we don't just have to do print
  • 00:02:51 statements in here we can make this more
  • 00:02:52 complex so let's say I just wanted to
  • 00:02:54 print out odd numbers
  • 00:02:57 for example so I just wanted to print
  • 00:02:58 out odd numbers I could do something
  • 00:03:00 like if I mod to so this means like
  • 00:03:06 divided and check the remainder equals
  • 00:03:08 equals one so that would make it odd do
  • 00:03:12 a colon two because whenever we have
  • 00:03:13 something inside we always have to use a
  • 00:03:15 colon and indent and then I'll do we
  • 00:03:20 don't have to use an else but I can do
  • 00:03:22 an else else we'll just do nothing just
  • 00:03:24 we can write this pass word as you can
  • 00:03:29 see there 1 3 5 7 and 9 so we can make
  • 00:03:34 our four loops a little bit more complex
  • 00:03:35 but this eye is just like a placeholder
  • 00:03:39 so if I instead is this is just like
  • 00:03:42 variables you know we can call whatever
  • 00:03:44 we want is just going to be holding the
  • 00:03:46 variable for us so like when we were
  • 00:03:47 writing functions and we passed in a
  • 00:03:50 variable to the function name this is
  • 00:03:55 the same thing it's just a placeholder
  • 00:03:56 so I could call this like cactus for
  • 00:04:00 cactus and range 10 print cactus and as
  • 00:04:03 you can see does the 0 to 9 just like we
  • 00:04:07 had for when we just did print I one of
  • 00:04:11 the most useful reasons we use a for
  • 00:04:13 loop is to iterate through lists so as
  • 00:04:16 you should have learned in the last
  • 00:04:17 video about lists we can declare lists
  • 00:04:20 such as a grocery list by doing the
  • 00:04:23 following use these two brackets and
  • 00:04:25 then insert our items inside so let's
  • 00:04:27 say we wanted milk toast eggs and
  • 00:04:36 avocados so that's our grocery list
  • 00:04:40 instead of doing for cactus and range 10
  • 00:04:42 I would actually change this to in
  • 00:04:46 grocery list for cactus in grocery lists
  • 00:04:49 and cactus does not make the most sense
  • 00:04:52 to use here so we're going to just say
  • 00:04:54 for item in grocery list it doesn't
  • 00:04:57 matter what we call it as long as we use
  • 00:05:00 the same value inside the for-loop so
  • 00:05:03 for item in grocery list this will take
  • 00:05:06 the list defined by the grocery list
  • 00:05:09 and just take the first element then the
  • 00:05:12 second element then the third element
  • 00:05:14 and then the fourth element and each
  • 00:05:15 time we get a new element we can access
  • 00:05:18 that element by this right here so if I
  • 00:05:21 run this as you can see milk prints out
  • 00:05:23 my online toast prints out online eggs
  • 00:05:25 prints out of one cut line and avocadoes
  • 00:05:27 prints out on one line
  • 00:05:29 so this iterated through a list and we
  • 00:05:32 don't have to just use you know words we
  • 00:05:35 can also do this with numbers so let's
  • 00:05:37 say I wanted to pass in some weights so
  • 00:05:40 I'm I'm gonna just I took this this
  • 00:05:43 information from online but I'm passing
  • 00:05:45 in the weights of some players on a
  • 00:05:48 professional sports team and bonus
  • 00:05:51 points to the person who watches this
  • 00:05:53 video and can figure out which sports
  • 00:05:55 team these values are coming from so
  • 00:05:58 here are the values so we have weights
  • 00:06:01 these are the weights of all the players
  • 00:06:02 on this sports team as you can see
  • 00:06:06 there's a bunch of them what we can do
  • 00:06:09 with that is we can iterate through each
  • 00:06:11 weight in that list with this for loop
  • 00:06:14 so for wheat in weights and if I print
  • 00:06:22 out the weight as you can see below it
  • 00:06:28 grabs each of want each one of them and
  • 00:06:30 it prints it out on a new line all right
  • 00:06:34 so this is cool but we're usually going
  • 00:06:36 to want to do something more complex
  • 00:06:37 than simply then simply print out like a
  • 00:06:42 single value we want to do something
  • 00:06:44 with these values so for example let's
  • 00:06:47 say we wanted to find the average weight
  • 00:06:49 of a player on this team so if I did
  • 00:06:53 average weight so how do we calculate
  • 00:06:57 average weight so if I was doing this
  • 00:07:00 mathematically I take each one of these
  • 00:07:02 values and weights add them all up and
  • 00:07:04 then divide by the total number of
  • 00:07:07 weights there are so what I can do is
  • 00:07:09 I'm going to define a variable called
  • 00:07:11 total weight outside of this for loop
  • 00:07:14 and total weight before we take any of
  • 00:07:18 these numbers from the players it's just
  • 00:07:20 going to be set to zero
  • 00:07:23 what we're gonna do is on each iteration
  • 00:07:26 we're gonna add the weight that we're
  • 00:07:30 currently looking at so in the first
  • 00:07:31 iteration we would add 208 to our total
  • 00:07:35 weight so I can do total weight equals
  • 00:07:37 total weight plus weight so this takes
  • 00:07:43 our cumulative sum and adds the one
  • 00:07:46 we're currently looking at to it and
  • 00:07:48 then if we get to the bottom I'm just
  • 00:07:50 going to print out total weight and the
  • 00:07:58 total weight of all these players is
  • 00:07:59 just under 5,000 pounds so if you ever
  • 00:08:02 to line them all up squish them together
  • 00:08:03 and put them on a scale that's what the
  • 00:08:05 team would weigh so and also worth
  • 00:08:09 noting is I don't have to do total
  • 00:08:11 weight equals total weight plus weight
  • 00:08:13 there's actually a shortcut to do this
  • 00:08:15 command and that is plus equals so
  • 00:08:17 instead of doing total weight equals
  • 00:08:19 total weight plus weight if I just do
  • 00:08:21 total weight plus equals weight it takes
  • 00:08:23 whatever total weight was set before and
  • 00:08:25 adds weight to it and then resets it to
  • 00:08:28 total weight so if I run this it's still
  • 00:08:31 the same thing all right and then to get
  • 00:08:34 the average weight of a player we just
  • 00:08:36 need to know the total number of weights
  • 00:08:38 there are in this list and from the last
  • 00:08:42 video you might remember the command
  • 00:08:47 length le n so we can use the command
  • 00:08:53 number of players will call equals
  • 00:08:57 length le n of the weights variable so
  • 00:09:03 now if I print out number of players we
  • 00:09:09 have this amount of pounds for this
  • 00:09:11 amount of players and finally what we
  • 00:09:14 wanted to do was the average weight of a
  • 00:09:16 player on this team and we can do print
  • 00:09:19 total weight divided by number of
  • 00:09:23 players so this is a useful way we can
  • 00:09:26 kind of use statistics use Python to aid
  • 00:09:29 in our statistics so the average weight
  • 00:09:33 of a player on this team is 109
  • 00:09:36 nine pounds and we can easily adapt that
  • 00:09:42 you know keep going farther I'm gonna
  • 00:09:44 not do it in this video just for the
  • 00:09:46 sake of time but we could keep going we
  • 00:09:48 could find the standard deviation of a
  • 00:09:56 dove this process also I want to mention
  • 00:10:00 real quick that this method that we did
  • 00:10:02 right now does for weight and weight and
  • 00:10:05 it grabs the individual weight so if I
  • 00:10:08 was printing out each weight you should
  • 00:10:10 have seen this before but I'll do it
  • 00:10:12 again it going from the top starts with
  • 00:10:17 208 then 221 just as this goes another
  • 00:10:21 way to do this is by instead of using
  • 00:10:23 the actual weight we find the index in
  • 00:10:29 the range of the length of weights so
  • 00:10:37 what I'm doing right now is instead of
  • 00:10:38 directly grabbing this toy I'm instead
  • 00:10:41 just setting an index that will contain
  • 00:10:45 all the indexes in however long this
  • 00:10:48 weights is so now I can print out index
  • 00:10:55 and this is going to give me there
  • 00:10:58 because I didn't define weight now they
  • 00:11:01 say weight is zero for now don't worry
  • 00:11:03 about this so but what you can see is
  • 00:11:07 I'm printing out the index it's counting
  • 00:11:09 0 to 24 which represents the 25
  • 00:11:12 positions a player can have in this
  • 00:11:14 graph and what we can do is with that
  • 00:11:18 index we can grab a weight as follows we
  • 00:11:21 can do total for weights that's the list
  • 00:11:24 of the weights and then do brackets and
  • 00:11:27 then index so we're grabbing the weight
  • 00:11:31 that is found in the index position so
  • 00:11:34 when this is 0 on the first iteration we
  • 00:11:37 grab weights 0 and that is 208 so this
  • 00:11:40 is just another way to do the same exact
  • 00:11:42 thing we just I just showed you before
  • 00:11:44 and sometimes this can be a useful way
  • 00:11:47 to do it now
  • 00:11:48 always but it is good to see that you
  • 00:11:52 can do this method and just grab the
  • 00:11:54 indexes and then set it down here so now
  • 00:11:57 if I run this we have that same value we
  • 00:12:00 had before and just keep note of this
  • 00:12:09 because maybe we can use this in the end
  • 00:12:13 of the the video you'll need to grab
  • 00:12:15 multiple indexes at the same time so if
  • 00:12:18 we do it this method we can do like
  • 00:12:21 weight – equals weights of index plus
  • 00:12:26 one and we can grab two different
  • 00:12:28 weights at the same exact time so just
  • 00:12:30 note this method before I finish with
  • 00:12:36 for loops
  • 00:12:37 one of the advanced methods that I guess
  • 00:12:40 it's not that advanced but one thing
  • 00:12:43 that is this help helps me a lot when I
  • 00:12:45 may be iterating through an Excel
  • 00:12:48 spreadsheet is using this enumerate
  • 00:12:50 keyword so if I use this enumerate
  • 00:12:54 keyword I can grab the index and the
  • 00:12:58 weight at the same time it gives me both
  • 00:13:00 so I do like index comma weight that
  • 00:13:02 will give me the index of this position
  • 00:13:04 and also the actual value at that
  • 00:13:07 position which is sometimes helpful to
  • 00:13:09 to know both of those things so if I do
  • 00:13:11 that and I print index and then I print
  • 00:13:14 weight and I run it down here you should
  • 00:13:19 see zero-two ou812 21 so this is a
  • 00:13:23 numerate key where it allows us to get
  • 00:13:25 both the index and the weight all right
  • 00:13:27 let's move on to wild loop so I'm going
  • 00:13:28 to delete all this code here alright to
  • 00:13:32 introduce wild lips basically think of a
  • 00:13:34 while loop as an if statement that
  • 00:13:36 repeatedly checks itself over and over
  • 00:13:39 again so if I did something like if true
  • 00:13:43 let's just say then I printed like hello
  • 00:13:47 world right and I ran that it would just
  • 00:13:52 print out that hello world one time a
  • 00:13:54 wild state a while loop however does the
  • 00:13:59 following so I did while true
  • 00:14:02 hello world this is basically like plain
  • 00:14:03 English like what would you think would
  • 00:14:05 happen while something was true and you
  • 00:14:08 had it inside that it just repeatedly
  • 00:14:11 prints out hello world so you can't let
  • 00:14:14 me change up this example a little bit
  • 00:14:15 so you can see more visually but I
  • 00:14:17 printed hello world and then hi again
  • 00:14:21 two different lines you see that it just
  • 00:14:25 is continually running these these two
  • 00:14:27 print statements over and over and over
  • 00:14:30 again and that's going to keep happening
  • 00:14:31 until this true becomes false so to show
  • 00:14:36 you an example where that type of thing
  • 00:14:38 would happen I'm going to cancel this
  • 00:14:40 real quick so if you're go to tools you
  • 00:14:42 can cancel a a while loop if it's
  • 00:14:45 infinitely running this is called an
  • 00:14:47 infinite loop you can cancel it by going
  • 00:14:49 into your tools and then cancel build or
  • 00:14:51 control slash break control + play break
  • 00:14:54 or command + + + I literally can't talk
  • 00:14:58 right now
  • 00:14:58 command + break if you're on a Mac so
  • 00:15:01 let's imagine this is just like a for
  • 00:15:04 loop so in a for loop we can count to 10
  • 00:15:06 we can also do the same thing in a wild
  • 00:15:08 lip so let's say I said what I equals
  • 00:15:10 zero while I is less than 10 print I and
  • 00:15:16 then we did I plus equals one so I ran
  • 00:15:21 this I print out zero through nine
  • 00:15:25 because it keeps printing out I when
  • 00:15:29 it's less than ten and because we keep
  • 00:15:31 adding one two I each iteration
  • 00:15:33 eventually it will get to ten and this
  • 00:15:36 statement right here will be false so I
  • 00:15:40 could also like print out I less than
  • 00:15:44 ten so you see how execute is true all
  • 00:15:48 the time here and if I and it didn't
  • 00:15:51 execute that last one that's false
  • 00:15:52 because if this is true or this becomes
  • 00:15:57 false
  • 00:15:58 it doesn't execute any of the code
  • 00:15:59 inside all right so that's counting to
  • 00:16:04 ten with a while loop one of the major
  • 00:16:07 reasons I guess a while loop is used is
  • 00:16:10 to deal with these boolean conditions so
  • 00:16:12 if we're developing a game
  • 00:16:14 we might have a variable called game
  • 00:16:16 over right and that's initially set to
  • 00:16:19 false so we could have a while loop
  • 00:16:21 running they would continually run and
  • 00:16:23 execute our gameplay code while we're
  • 00:16:26 not at the game over condition so I just
  • 00:16:31 print playing game here I never run that
  • 00:16:36 you see it just keeps I can scroll up
  • 00:16:39 and it keeps playing the game this just
  • 00:16:42 is happening non-stop so let's imagine
  • 00:16:45 in addition to playing like in addition
  • 00:16:47 to having this not game over a condition
  • 00:16:49 we had a score and let's say the goal of
  • 00:16:50 this game was to get to 100
  • 00:16:52 so if our score is initially set to 0
  • 00:16:56 let's say this is a super fun game you
  • 00:16:59 know each iteration you get a a random
  • 00:17:06 value so if we import random this the
  • 00:17:10 score at each iteration is just going to
  • 00:17:13 be some random integer that we add on to
  • 00:17:15 the score so score plus equals random
  • 00:17:19 end and let's say it's like 1 through 10
  • 00:17:22 so if you don't remember a random and on
  • 00:17:24 randon this is something I showed in the
  • 00:17:25 math and variables video the first
  • 00:17:28 actual tutorial and I'll print score and
  • 00:17:32 I'll say if score is greater than 100
  • 00:17:37 game over equals true so watch what
  • 00:17:43 happens here
  • 00:17:44 so our score just keeps increasing then
  • 00:17:49 finally it hits 102 102 is the first
  • 00:17:51 value that is greater than 100 so this
  • 00:17:54 variable becomes true and we exit out of
  • 00:17:57 this while loop so while loops help us
  • 00:18:00 like control flows based off a certain
  • 00:18:03 boolean condition so this game over
  • 00:18:05 because it's true or false it's a
  • 00:18:08 boolean condition before we finish up
  • 00:18:11 with the explanations of what loops are
  • 00:18:13 and we actually move on to two examples
  • 00:18:14 using loops I want to quickly go over
  • 00:18:18 two more things and that is the break
  • 00:18:19 statement in the continue statement so
  • 00:18:22 imagine we had in
  • 00:18:24 addition to this code right here we had
  • 00:18:25 a line that just said game is still on
  • 00:18:29 right and this could be anything maybe
  • 00:18:31 this is game crucial code right down
  • 00:18:34 here and it comes out for this if score
  • 00:18:37 is greater than 100 but note that when
  • 00:18:40 this game is over at 105 so the game
  • 00:18:44 over is true right there at this 105
  • 00:18:47 block it still prints out game is still
  • 00:18:49 on so it could be still printing out or
  • 00:18:52 executing this game game code so we'll
  • 00:18:56 use the break statement if we want to
  • 00:18:57 just quickly exit out of exit out of a
  • 00:19:02 while or for loop this works with both
  • 00:19:04 for loops and while loops so I could do
  • 00:19:07 break and then if I reran that you see
  • 00:19:10 how 105 is the last value but it does
  • 00:19:12 not print this game is still on here so
  • 00:19:16 it exits out of the while loop so it
  • 00:19:19 would exit out and go to whatever line
  • 00:19:21 is at the first indent so if I printed
  • 00:19:26 you lose here or actually maybe you win
  • 00:19:29 let's say you win see that it doesn't
  • 00:19:34 print the game is still on but it does
  • 00:19:36 print this you win all right so that's
  • 00:19:38 the break statement it exits out of a
  • 00:19:40 four or a while loop really quickly so
  • 00:19:42 right when it's hit then you're out of
  • 00:19:45 the loop the other statement is the
  • 00:19:47 continue statement so maybe a better way
  • 00:19:52 to see the continue statement is if we
  • 00:19:54 had in addition to score is greater than
  • 00:19:59 greater than 50 we had a walk that was
  • 00:20:02 if score is less than 50 and I wanted to
  • 00:20:05 just write continue and then down here
  • 00:20:09 I'm gonna leave this break but I'm gonna
  • 00:20:12 print out some code that just says you
  • 00:20:14 are getting close to winning so what
  • 00:20:18 this continue statement is gonna do is
  • 00:20:21 during the first few iteration so we got
  • 00:20:24 2 3 13 20 23 29 31 to 36 46 50 doesn't
  • 00:20:29 print out anything but then once we hit
  • 00:20:33 50
  • 00:20:33 it says you're getting close to winning
  • 00:20:35 53 says you're getting close to winning
  • 00:20:37 so what this continuous statement did is
  • 00:20:40 it exits out of the loop but it exits
  • 00:20:45 out to the top of the loop so it it
  • 00:20:48 doesn't run any of the code down here
  • 00:20:50 but it run goes back to the top so the
  • 00:20:54 difference between the continue
  • 00:20:56 statement and the break statement is
  • 00:20:57 that the continuous statement goes back
  • 00:20:59 up top to the next iteration of the loop
  • 00:21:02 the break statement exits out of the
  • 00:21:05 loop so you'll find uses of these
  • 00:21:09 different statements throughout your
  • 00:21:11 like well as you get more experience
  • 00:21:14 with these loops I just want to
  • 00:21:15 introduce them so you knew that they
  • 00:21:16 existed okay let's finish this video off
  • 00:21:19 with two examples on the coding bat
  • 00:21:21 website that I've introduced before so
  • 00:21:23 we'll do I'm gonna drag in the coding
  • 00:21:26 bat site come on so we're gonna do
  • 00:21:30 coding back Python and we're gonna do
  • 00:21:33 the warm-up two exercises so the first
  • 00:21:37 exercise we're going to do is called
  • 00:21:39 string x so try this exercise on your
  • 00:21:46 own and then if you can't figure it out
  • 00:21:47 I'll do it once you impose the video
  • 00:21:53 okay to do this first exercise we need
  • 00:21:57 that we note that whatever is in here we
  • 00:21:58 need to be repeated this many times so
  • 00:22:02 the amount of times we need to repeat
  • 00:22:03 something we'll use a for loop for that
  • 00:22:05 so I'm going to do for I in range and
  • 00:22:10 colon and then I will do print string
  • 00:22:18 right because that's how many times we
  • 00:22:20 want whatever oh my got to keep doing
  • 00:22:21 ctrl s but if I use this site that so
  • 00:22:24 many times we want to print out whatever
  • 00:22:27 strings in here and that's represented
  • 00:22:29 by this variable and then the amount of
  • 00:22:30 times is represented by the
  • 00:22:32 very well so if I run that it says don't
  • 00:22:36 print compute and return or result value
  • 00:22:38 so remember whenever you use a function
  • 00:22:40 we don't want to print we want to return
  • 00:22:44 a value so I'm going to do a return
  • 00:22:46 string this actually doesn't work as you
  • 00:22:51 can see every time here in the run code
  • 00:22:54 it just does it one time so we don't
  • 00:22:57 actually want to return the string what
  • 00:22:59 we want to do is we want to set a string
  • 00:23:02 like we'll call it final string that
  • 00:23:05 equals we'll just say that's empty at
  • 00:23:10 first when I just say control this again
  • 00:23:13 so final string is s empty each time we
  • 00:23:17 run this loop we want to add on the
  • 00:23:20 string that we're gonna print out at the
  • 00:23:23 end to the final string so final string
  • 00:23:27 plus equals and I might have not have
  • 00:23:30 mentioned this before but you can add
  • 00:23:32 together you can concatenate strings
  • 00:23:34 using the plus the plus operator so just
  • 00:23:40 like you can add number to knotted
  • 00:23:41 strings but adding string just means
  • 00:23:43 putting like two words together so final
  • 00:23:48 string plus equals the string and then
  • 00:23:53 if I return that and I don't want to
  • 00:23:56 return it in this for loop because then
  • 00:23:58 it will return on the first execution I
  • 00:24:00 want to actually go outside the for loop
  • 00:24:02 and return it once the for loop is done
  • 00:24:04 so I'm gonna do final string and click
  • 00:24:08 go and everything works for that so
  • 00:24:11 start out with a blank string you run a
  • 00:24:13 loop that iterates n times and each time
  • 00:24:17 you append or I guess you're not really
  • 00:24:19 a pen but you add on a new string so
  • 00:24:23 that when you get to the end of the loop
  • 00:24:24 you have that string repeated n times
  • 00:24:29 cool
  • 00:24:33 okay let's do one more exercise we're
  • 00:24:35 gonna do beret one two three
  • 00:24:37 this is going to be a pretty hard
  • 00:24:38 problem array one two three so don't
  • 00:24:41 feel bad if you can't figure it out and
  • 00:24:44 pause the video and when you're ready to
  • 00:24:49 see the solution on pause alright to
  • 00:24:54 solve this problem we are given an array
  • 00:24:56 of int and we want to return true if the
  • 00:24:59 sequence of numbers 1 2 3 appears in the
  • 00:25:02 array somewhere so this is where we're
  • 00:25:05 gonna have to use that indexing that I
  • 00:25:07 show you when we were going through the
  • 00:25:08 player weights example earlier in this
  • 00:25:11 video so we have this array and if I
  • 00:25:14 just went ahead and printed let's say we
  • 00:25:18 had a loop and we're iterating through
  • 00:25:19 each number in the loop I just did for
  • 00:25:22 number in nums
  • 00:25:24 for num nums print num and I'll just
  • 00:25:30 return at the end return this will just
  • 00:25:34 just so it doesn't yell at me I'm just
  • 00:25:37 returning this this is not actually
  • 00:25:38 gonna work yet don't print I won't even
  • 00:25:43 let me okay it doesn't even let me print
  • 00:25:49 that's annoying but so if it if you're
  • 00:25:53 trying to test this and it's you having
  • 00:25:55 trouble because it won't leave print
  • 00:25:57 what I recommend you do is you can
  • 00:25:58 actually just copy this code and put it
  • 00:26:01 in your sublime text window so I'm
  • 00:26:06 deleting all this and I'm pasting it in
  • 00:26:09 here so for num nums i want to print num
  • 00:26:14 and i'll take one of the examples they
  • 00:26:18 gave me so we'll take this last example
  • 00:26:23 because this last example is a true
  • 00:26:25 example so we got this is our nums
  • 00:26:38 and we have the function definition now
  • 00:26:42 we need to actually call the function so
  • 00:26:44 we're in the array one two three of nums
  • 00:26:49 and as you can see we're printing out
  • 00:26:51 each value in that list by just doing
  • 00:26:53 what we have here so the tricky thing is
  • 00:26:58 is in this problem we need to keep track
  • 00:27:01 of three numbers at the same time
  • 00:27:04 because we're looking for one two three
  • 00:27:07 so what we should do instead of doing
  • 00:27:09 for num nums we should do for I in range
  • 00:27:16 nomes and what that allows us to do is
  • 00:27:21 we can do something like first value
  • 00:27:25 equals nums
  • 00:27:26 I that's the I index second value equals
  • 00:27:32 nums I plus one so that would be if this
  • 00:27:36 I was 0 we'd have 1 + 0 plus 1 would be
  • 00:27:39 the first index so that would give us
  • 00:27:41 these two values then finally third
  • 00:27:44 value equals nums I plus 2 and basically
  • 00:27:51 what we're checking is if first value
  • 00:27:54 equals equals 1 and second value equals
  • 00:28:00 equals 2 and third value equals equals 3
  • 00:28:06 then we want to return true right and if
  • 00:28:12 that never happens then we know at the
  • 00:28:15 end of this week and so where this
  • 00:28:17 statement is so after the loop is done
  • 00:28:19 we can return false if we brought that
  • 00:28:23 list object cannot be interpreted as an
  • 00:28:27 integer so I can do length knows so
  • 00:28:29 before I was trying to take the range of
  • 00:28:34 a list I need to actually take the range
  • 00:28:37 of the length of the list
  • 00:28:39 we're trying to take the range of run
  • 00:28:40 that and we'll actually print out this
  • 00:28:43 page so we'll print out to see if it
  • 00:28:45 says true or false true cool so it read
  • 00:28:52 that we had the 1 2 3 here because we
  • 00:28:56 got the three indexes and the one thing
  • 00:28:59 to note with this is we're still not
  • 00:29:00 done because if I ran this function into
  • 00:29:04 our coding bat so it's actually gonna
  • 00:29:09 give us an error so see if you can
  • 00:29:13 figure out the error so it says list
  • 00:29:14 index out of range and the reason we
  • 00:29:21 have the error is when we get to the
  • 00:29:23 last index so let's say we were looking
  • 00:29:25 at this bottom example again or maybe
  • 00:29:28 that's say we were looking at this
  • 00:29:29 middle example in this middle example we
  • 00:29:33 don't actually have a one two three we
  • 00:29:36 have one one two four one so we would
  • 00:29:38 iterate
  • 00:29:38 starting at this element then go to this
  • 00:29:40 element then go to this element then go
  • 00:29:42 to this element and then so when it got
  • 00:29:45 to this element the first value would be
  • 00:29:47 four second value would be one but then
  • 00:29:51 we're trying to get the third value
  • 00:29:52 using this line right here and there
  • 00:29:55 wouldn't actually be enough list get
  • 00:29:57 that third value so this line right here
  • 00:29:59 would cause us an error so what we
  • 00:30:03 actually have to do is do length nums
  • 00:30:06 minus two because we want to stop right
  • 00:30:12 at this the last time we can get three
  • 00:30:16 elements in a row because we don't need
  • 00:30:17 to check any farther so you want to stop
  • 00:30:20 three before the end so in this case
  • 00:30:22 we'd stop in this element this one we'd
  • 00:30:24 stop at this element and this one we'd
  • 00:30:26 stop at this element so if I run that
  • 00:30:28 yeah we got it to work
  • 00:30:30 cool all right that's all we're going to
  • 00:30:35 cover in this video hopefully you
  • 00:30:37 learned what for loops and while loops
  • 00:30:39 are if you have any questions still
  • 00:30:41 leave them down in the comment section
  • 00:30:44 and also make sure to check out that
  • 00:30:46 game I showed you at the start of this
  • 00:30:47 video so the game that looked like
  • 00:30:53 yes Wow where is it where the game guy
  • 00:30:56 oh yeah so make sure to check out this
  • 00:30:58 game there'll be a video on how to build
  • 00:31:01 that in the description alright thanks
  • 00:31:06 for watching guys make sure to LIKE this
  • 00:31:09 video if you learned something and also
  • 00:31:11 subscribe so you don't miss the next
  • 00:31:13 tutorial which I'll be posting sometime
  • 00:31:15 next week peace