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