#100DaysOfCode in Python Transcripts
Chapter: Days 19-21: Iteration with itertools
Lecture: Traffic Lights Project

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Alrighty, in this video we are going to create
0:03 some traffic lights.
0:05 As discussed in the ReadMe from the three-day overview,
0:09 I strongly urge you to try this yourself.
0:12 It's not too difficult, it's a nice little challenge
0:15 for your second day.
0:17 This video is going to walk you through
0:18 how I've created it.
0:22 If you haven't done it,
0:23 if you haven't attempted it yourself,
0:25 just pause it here, or hit stop and minimize.
0:28 Avert your eyes, children, and just give it a try yourself.
0:32 This is the best way to learn.
0:34 You're going to need itertools cycle, I'll give you that tip,
0:38 and that's it.
0:40 Out of all the stuff we've covered so far,
0:42 itertools cycle is all you're going to need for this.
0:46 Then, just think about how traffic lights work
0:48 and go from there.
0:50 Now, into the code.
0:53 We're going to just import some modules here.
0:56 For me, I'll discuss this in a minute, we're going to import
1:01 from time import sleep because we do want our traffic lights
1:06 to sleep when you're going between the colors.
1:13 Now let's import itertools
1:15 and let's import random
1:19 because I have an idea.
1:22 First thing we're going to need is we're going to need
1:24 our colors, aren't we?
1:25 What are the three colors on a traffic light?
1:28 We've got red, green, amber, or yellow,
1:32 whatever you want to call it.
1:35 You know what I mean, I can't type this bit, can I?
1:38 And that gets our colors list.
1:41 Now, the rotation between those colors.
1:45 Again, if you haven't done this yet, hit pause now.
1:51 The rotation of going through those lights,
1:53 we're going to use cycle, remember the spinny thing
1:56 from the other video.
1:57 We're going to use itertools.cycle, but this time
2:02 we're going to call colors,
2:06 just like we have here.
2:08 We're calling this and we know it's now going to cycle
2:11 through red, green, and amber because by using split
2:14 we created a list of these three strings.
2:18 So that's what rotation is.
2:21 Alrighty, so let's create our function.
2:25 You know what, before we start any of that,
2:28 let's throw this in so we know where we're starting.
2:33 What do we want our function to be called?
2:36 Let's call it light rotation.
2:42 We're going to pass in rotation.
2:49 Not that, well, I suppose, we have to,
2:50 but we'll just do that anyway.
2:53 We'll go def light(rotation)
2:57 We're reading in the rotation.
3:02 So, what's this app going to do?
3:03 What's this little traffic light going to do?
3:05 It's pretty much going to have three if statements.
3:10 It's going to be, what do we see when it's amber?
3:12 What do we see when it's red?
3:14 And what do we see when it's green?
3:18 So, we'll create a for loop for color
3:23 in rotation.
3:27 Now, remember, that's pretty much,
3:28 we're not saying this here, we're saying just for the item
3:33 in rotation, and rotation is returning each one of these,
3:38 for the item in rotation, for the color in the rotation.
3:42 Let's just make it really simple.
3:44 If color equals amber,
3:47 this is a direct match now,
3:50 we're asking for a direct match.
3:51 At this point, we have to know
3:53 it's going to say exactly that.
3:56 Let's go print.
3:58 Caution.
4:01 The light
4:03 is...
4:05 Where's percent?
4:07 Okay.
4:10 And then we just close it off with color.
4:14 This is now going to print, if the color is amber,
4:17 caution, the light is amber.
4:22 Then, at this point, we want it to sleep
4:27 because, if you think about it,
4:29 when a traffic light goes yellow, or amber,
4:32 it doesn't just go to red straight away, does it?
4:35 It takes a few seconds, so let's just put in a sleep
4:39 of 3 seconds.
4:42 That's it for that.
4:44 Just because we're only dealing with 3 colors here,
4:48 let's just go with an elif statement.
4:50 If the color is, now, red, or, elif color is red.
4:55 We're going to go print, stop,
4:59 the light is...
5:03 Oops.
5:05 S and then we go color.
5:11 Now we can do a sleep of, let's go 2 seconds.
5:18 And then we can go else because in any other case
5:21 it's going to be green, isn't it?
5:23 Print, go.
5:26 The light is...
5:30 Oops.
5:31 Color.
5:34 And we'll do another sleep of 3 seconds.
5:37 Believe it or not, that's our app.
5:40 This function here is going to go
5:44 through editor's dot cycle of colors, red, green, and amber,
5:48 and it's going to say, well, if amber, do that.
5:51 If red, do that.
5:53 And then, if all else fails, it's going to be green,
5:56 it's going to be the other option,
5:57 and we're going to get green.
6:01 Let's run that.
6:03 Save it, F5 it.
6:05 Stop, the light is red.
6:07 Go, the light is green.
6:10 Caution, the light is amber.
6:12 Now, the catch here is that it's going to actually be
6:15 the same few seconds every single time,
6:19 so we can anticipate that.
6:20 That's not how traffic lights work.
6:22 So to make it a little more interesting,
6:26 a little more complicated, let's get rid of these static
6:32 sleep seconds.
6:33 We know with yellow lights, those are always going to be
6:36 3 seconds, or 5 seconds,
6:38 because it's the standard every time.
6:41 This is why I imported random at the start.
6:44 Let's make it a little randomly generated timer.
6:51 We're going to go return, random.randint.
6:56 And what are we going to put the...
6:58 See, here we go, return a random integer in the range.
7:00 So what do we want the range to be?
7:03 Let's go 3 to 7 seconds.
7:08 So it's going to return those integers, anywhere between
7:12 three and seven, so we should just call this
7:16 in sleep
7:20 right there
7:21 and right there.
7:22 What that will do is it's going to generate a random number
7:26 between 7 and 7 every time, and this should change.
7:31 You can't predict what that's going to be.
7:34 Let's run it.
7:35 The light is red, okay.
7:38 We have no idea how long it's going to take.
7:40 There we go, so it actually took longer than before,
7:42 same with the light being green.
7:45 That was very quick, that was only about 3 seconds.
7:47 Amber, default, 3 seconds.
7:49 Then we're back on red, and it's sitting there
7:53 for quite a while, so there you go.
7:54 Now this is more of an accurate street traffic light,
7:59 where it's a bit more randomly generated.
8:01 But all of this,
8:04 due to the awesomeness of itertools.cycle.
8:09 There's your traffic light.
8:10 Hopefully, your code looked something like that.
8:14 Hopefully it was a little cleaner.
8:16 But other than that I think we've just made an awesome
8:19 little nifty app and a little tool, something that you'll
8:23 probably never use, but that's itertools.cycle.