Python for the .NET Developer Transcripts
Chapter: OOP: Object-Oriented Python
Lecture: Parking a car

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Now that we have our parking lot and over here, we were able to create it. Let's go and actually park our cars.
0:08 We've got to get them out and drive them around have lots of fun and we fuel them back up. It's time to park 'em. I'm going to say for c in cars
0:15 lot.park, and we'll just pass the car over. Now lot's just lot, like this, easy enough. Notice there's a little marker here from high term saying
0:24 it doesn't exist, but we can add a method and it will automatically write it. Notice it does itself, say car, say car like that.
0:33 So now what are we going to do? How do we get this to park the car? Well, the job that really we have to do
0:38 is find an empty spot and then put the car in it. So what we have to is we have to say for i in self.spots. Now if we just loop over this
0:47 this will just give us the keys back and that's okay but I'd like to also have the value as part of the loop without an extra step.
0:54 So I can say dot items, then if I just print let's say print I like that, if we run that this is actually a tuple. What it is, is a key and the value.
1:06 So I'm going to say k, v and do tuple unpacking. And do that, and then we won't really need this, you'll see.
1:14 So if we run it, you can see the version of the keys and then that would either be car or right now it's none. So then it becomes super easy.
1:23 We just say if not v, or you could say v is None this may be a better way to say it if that's true then we want to put a car into that spot.
1:32 Then we just say self dot spot of that key as that car. Break, can go over here. And let's just do a quick print of the lot.spots.
1:46 And we can actually use this thing called pp for pretty print to look at this a little bit better, like this.
1:52 It prints out dictionaries kind of like nice JSON-style. Over here, here's our dictionary structure. A1 to A4, the first level is all booked out.
2:02 I guess A5 is left, but here's our sports car basic car, electric car, electric car. These are parking in a very orderly fashion.
2:09 Let's do like one more, let's do two more cars just so we can, let's see what we got. We can have a Leaf, I want this at 30.
2:17 And over here let's do a Camero, probably let's put 40. Just going to take a guess. So now we run it one more time.
2:26 We can see we're making our way up to the second level. We're parking our electric cars up there. That's, I guess that would be our Leaf, wouldn't it?
2:34 Okay, so we're able to park our cars, that's really cool. And we'll go ahead and comment this out 'cause we're not going to really want that.
2:41 But here's how we can create this function this factory method and then use the thing that got created here.

Talk Python's Mastodon Michael Kennedy's Mastodon