Display the List7:43 with Craig Dennis
Let's wrap up this application and review it in its entirety.
Okay, first thing's first, I'm gonna wipe our all these instructional columns. 0:00 Let's get rid of all of these, clean that up a bit. 0:05 Let's get a little noisy. 0:08 All right, awesome. 0:11 So, that feels better. 0:13 Let's look at what we have left. 0:15 All right so we left done here because we want to print the list out in 0:18 totality when it's done. 0:21 We wanna show the list and there's this one here too. 0:22 I wanna see the list any time so I can verify my order. 0:25 So let's make a new command for that. 0:28 That sounds good, all right? 0:31 So let's come back over here. 0:32 So first, I'm gonna clean this up a little bit. 0:33 I'm gonna move this add to list down below the show help and 0:36 typically what happens in between functions, 0:41 you'll see that the starts getting busy is you leave two spaces. 0:44 So there we, so we left that two spaces between the two there. 0:48 Before and after, there you go. 0:52 All right, so we want to define a new function that's named show list. 0:55 So let's do that. 1:03 So we'll say, define a function 1:03 named show_list that prints 1:10 all the items in the list. 1:15 That sounds good, right? 1:21 You got this, right? 1:23 You can loop through all of them and print them out, right? 1:24 You got this, right? 1:27 Pause me. How did it go? 1:28 Here's how I did it. 1:31 So I said def show list. 1:32 Here's your list, nice little heading there for it. 1:40 And we're gonna loop through that list. 1:45 So for each of the items In the shopping list, we are going to print said item. 1:48 Awesome, and now, we want to be able to have this called. 1:56 So let's make the command SHOW, in all upper case. 2:04 So let's put it right here. 2:08 We'll say Enable the SHOW command, 2:09 cuz we wanna call that function SHOW command to show the list. 2:16 Don't forget to update the help documentation. 2:22 This is super important. 2:26 Sometimes you'll add a new command and 2:27 you'll forget that you've already made this help. 2:29 And we have the help at the top of the file, so don't forget to do that and 2:31 I wanna drop a hint here for you. 2:35 Make sure to run it. 2:38 Make sure that you run this after you had that show command. 2:41 There's a little gotcha if you forget something. 2:44 So make sure you run it, and just make sure your list looks right. 2:46 Are you ready? 2:50 Pause me. 2:50 Did you catch it? 2:52 Here's what I did. 2:53 So I went ahead and it's another else if. 2:54 And if this is equal to our command of show, we call the function. 2:57 But we got to remember to use our control flow, right? 3:05 We gotta call continue and 3:09 if not, what will happen is show will end up in your grocery list. 3:11 When you embrace flow control like break and continue, 3:16 it's super important that you pay attention to all the used cases. 3:18 All right, we are so close to done. 3:21 Now, we need to show the list on completion. 3:24 So what we can do is when this while loop is over while this while true when 3:27 this is over. 3:32 We can just call it. 3:34 We will call show_list. 3:38 Awesome let's give that a run. 3:43 Let's say, Python shopping list. 3:45 Look what I forgot to do. 3:55 I didn't update the help, so I don't know about show. 3:57 I'm gonna do that. 3:59 Let me get back up in there. 4:00 Let's go back up in here. 4:01 So we'll add enter SHOW 4:04 to see your current list. 4:11 Awesome, whew that was close. 4:15 Okay so we're gonna run Python shopping_list.py. 4:18 There's the show which we pick up at the store. 4:24 Well obviously we need some tortillas. 4:29 Let's get some green chiles. 4:33 I need pork shoulder. 4:38 What am I forgetting? 4:41 What's on my list again? 4:42 What do I have here? 4:44 Right, tortillas, green chiles, pork shoulders, cilantro. 4:46 There we go and now I am DONE. 4:52 And check it out there is my final list, 4:54 you're totally invited to my taco fiesta, look at this. 4:57 We are all done, awesome. 5:01 Should we walk it one more time? 5:06 Let's do that. So I'm gonna come in here. 5:09 I'm gonna get rid of these instructional commands that we have in here. 5:10 Let's just walk it. 5:16 So we create our brand new list that the program will use in a global fashion. 5:18 And then our program is gonna accept multiple commands. 5:24 So we should have some detailed help, right? 5:28 Programs are no good if users can't use them. 5:30 So we print out our help, and then we have our add to list function. 5:32 And it appends a value to the end of our shopping list and 5:38 updates the user with the count. 5:41 And lists and most objects for 5:45 that matter can make use of the lean command, so pythonic. 5:46 And then we have our show list, and lists are iterable, 5:52 so we can loop through them with a four item in list. 5:57 Now, this is kind of strange, right? 6:01 So we have this while True. 6:05 One way to write loop that take many flows is to set things up 6:07 in an infinite loop matter. 6:10 While true is always gonna happen, 6:12 we rely on further control flow to control how things happen. 6:14 We take a single prompt and since it can be an item or 6:20 a command, we have made the prompt generic. 6:24 A little greater than sign, feels kinda good. 6:27 And then we just check each of our commands. 6:30 So here's the flow if the user is done. 6:33 Break out of the while loop, we are done with this while loop, 6:38 we don't anything more to do so break out of it. 6:41 Otherwise if we're looking for help, show it and remember to control the flow. 6:44 Calling continue will shoot us back up to this while true line which is no surprise, 6:51 still true. 6:56 What this avoids is us from continuing and adding help to our grocery list. 6:58 We don't want that. 7:02 Well, at least not from the grocery store. 7:03 Now, I of course, could have just put this add to list inside of an else and we could 7:05 remove the continues, but then I would've been able to show you the continue loop. 7:10 So I want you to be cautious of controlling the flow with break and 7:14 continue. 7:18 Break is known to break your program, 7:19 and then finally we add that new item to our function. 7:21 It comes into the add to list then boom, here we go. 7:26 So I hope that that all made sense, and 7:29 if there is anything at all that is still confusing you, feel free, rewind it, 7:32 watch it again and hit the forum if there's anything confusing at all. 7:36 You did an awesome job, enjoy the taco party. 7:40
You need to sign up for Treehouse in order to download course files.Sign up