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

Talk Python's Mastodon Michael Kennedy's Mastodon