Your First Python App: Build a Food Ordering App from Scratch [Beginner Walkthrough]
1:04:43

Your First Python App: Build a Food Ordering App from Scratch [Beginner Walkthrough]

Dataquest 27.03.2026 717 просмотров 28 лайков

Machine-readable: Markdown · JSON API · Site index

Поделиться Telegram VK Бот
Транскрипт Скачать .md
Анализ с AI
Описание видео
Build a complete food ordering app in Python using dictionaries, functions, and loops. This beginner-friendly project walkthrough is perfect if you're ready to move beyond isolated exercises and build a real, interactive application from scratch. Follow along with Dataquest's Director of Curriculum, Anna Strahl. Complete the project here: https://app.dataquest.io/c/161/m/718 What you'll learn: - How to use dictionaries to manage menu items, prices, and quantities - How to build reusable functions to organize your app's logic - How to implement loops and control flow for user interaction - How to structure a complete interactive Python application Key skills covered: Python dictionaries, custom functions, loops and conditionals, handling user input, structuring larger Python programs New to Python? Build the foundational skills you need with our Python Basics for Data Analysis course: https://www.dataquest.io/path/python-basics-for-data-analysis/ 00:00:00 - Introduction 00:08:48 - Setting up variables and menu data in Python 00:14:25 - Building a display menu function 00:20:40 - Writing an add-to-cart function 00:27:00 - Writing a remove-from-cart function 00:33:00 - Modifying cart items with an update function 00:34:30 - Displaying the cart with formatted output 00:39:29 - Building a checkout and total calculation function 00:40:40 - Handling user input 00:46:25 - Creating the main ordering loop 00:54:40 - Testing the complete food ordering app 00:59:53 - Audience Q&A [timestamps to be added]

Оглавление (12 сегментов)

Introduction

Hello, welcome to the Data Quest project lab. My name is Anna Stroll and today what we're going to do is we are going to be building a food ordering app. So, using basic Python skills, how can we create an experience for a user to see a menu of food items, select those items, and check out to purchase these items in this fictional app environment that we're going to create. So, a little bit about me, my name is Anna Stroll and I am the director of curriculum here at Data Quest. But, before I worked for Data Quest, I actually started my career as a high school math teacher and I did that for eight years. And then I transitioned to be a self-taught data analyst for 3 years and I used Data Quest to help me make that transition. So, uh if you are looking to get a career in data, you can do it because I did it. It is possible. Now, I do want to make sure that you're in the right place. Today's webinar will be focusing on Python and having some familiarity with Python basics like lists, loops, some conditional logic, that's your if else blocks, and dictionaries will help you understand the syntax we go through a little bit more quickly. But, if you are newer to these things, that's completely okay. This is a very beginner-friendly webinar. And a couple things that might be new to everybody, if you have seen these before, that's great. If you haven't, no worries cuz we'll talk about them is importing modules and the input function. And let's go ahead and dive into our agenda for the day. So, the first thing we're going to do is introduce the project. Then we're going to create something called global variables for our project. Then the majority of our time is going to be spent writing functions. We're going to create a lot of building blocks that will all funnel then into our main app loop. And we will end with some Q& A. So, make sure that you ask me those questions in the Q& A box. But, before we dive into the project, I always give this disclaimer cuz we always get asked this question. Why do this project by hand when AI can do the entire thing probably in the click of a single button. Well, that's not the point of what we're doing today. is to practice our Python programming. We're practicing the fundamentals. We're learning how to debug. If we get an error, we need to understand how to fix it, what to look for, what's happening in the code. And AI does get things wrong sometimes. It loves to make things up. It's very eager, but not always right. So, building our fundamental programming skills helps us later down the line for knowing what types of things AI might be confidently incorrect about. And finally, if you do want to add this to a portfolio, doing the code from scratch yourself helps ensure that you have a very deep understanding of the code. And you'll be able to talk about it more fully in an interview. All right, our project brief. So, let's imagine that you're on a local restaurant and you want to create an application where your users can order food directly from the app. If you've ever been to a restaurant, um like maybe a fast casual place, a lot of them have those large screens now where [snorts] you can make your food selection and purchase right there. That's basically what we're going to be creating. And so, the application should display the menu options and prices to the user. And the user should be able to add and remove items and update item quantities. So, let's dive in. I'm in the Data Quest environment here and if you are one of the folks who said you were new to Jupiter, this is the Jupiter Lab environment. When you first open it up, you'll see this side panel with things like lost and found, tmp, and then everyone will have a basics. ipynb file. To give us a little bit more space, I'm going to collapse this side panel by clicking on the folder icon. And it gives us a little bit larger working area. So, the very first thing we're going to do is define our global level variables. And this is variables for our app that are true throughout. So, for example, our restaurant in this scenario needs a restaurant name. So, our first variable is going to be restaurant name. I am calling mine the Hungry Hare. I don't know why. And something about global level variables is because they're useful and valid throughout the entire code that we're writing, the convention is to capitalize them like this. It doesn't change the way it works. It's just anyone else who's reading your code understands that this variable is accessible anywhere. So, that's our first global level variable. And the next variable we're going to create is our menu. So, our menu will include all of our food items and their prices. So, we're going to use a nested dictionary here. And I'm going to copy and paste the whole dictionary at once. It's going to look like a lot, but don't panic. It's something we're going to talk about. So, the reason we're using a nested dictionary is because for our back end of our app, we're going to want to refer to things by their ID number. Referring to something by the name itself when the name itself might change later down the line, that's too rigid. Whereas referring to it by its specific ID number that's never going to change, that's a way to ensure your code later down the line is going to be maintainable. So, SKU1 is an ID number and that's going to be the key for the first value in our that dictionary. So, our main dictionary has every key as the ID number and then the value for each of these is actually another dictionary. So, SKU1's value is hamburger. And we can see that the nested key is name with a value of hamburger. The nested key is price with a value of 6. 51. So, we have created, I believe, 10 different food items in my sample here. And each one follows that same flow. We have the ID number as the key in the main dictionary and then a small nested dictionary with the name and the price. Continuing along with our global level variables, let's think what else is needed for the entirety of our app? Well, we also need a list of app actions. So, that's going to be our next variable here, app actions. And I said list, that's actually incorrect. It's going to be a dictionary. And our dictionary is going to have the keys be a number that our user will be able to press eventually or press eventually. And the value is what corresponds what action corresponds with that number. So, if a user types one, then they're adding a new menu item to the cart. If they type two, they're going to be removing an item from their shopping cart. If they press three, they're going to modify an item. Four is going to be to view their shopping cart. Five will check out and six will exit the application. All right, we're going to have two more global variables. The next one is going to be a sales tax rate. In California, our sales tax rate is 7%. So, for percentages, we do have to keep them as decimal format here, but you can pick whatever sales tax rate you want. Usually sales tax, I think, is between 3% and 10%, but

Setting up variables and menu data in Python

your choice here. And the final variable is going to be the actual shopping cart itself. But, is there anything already in our shopping cart? No. So, we're going to initialize our cart as an empty dictionary. And this is all of our global level variables. So, to quickly recap, we have our restaurant name, we have our menu as a nested dictionary, we have our app actions, our sales tax rate, and our cart. I'm going to store this information in our Jupiter Lab and there's two ways we can do this. We can either click the play button while the cell is highlighted blue and this will run this part of our code. And we can tell it ran because it's no longer blue and there's a little one in this square brackets here. So, now we're going to start building the puzzle pieces. And each puzzle piece is going to be a user-defined function. The first puzzle piece user-defined function we're going to create is displaying the menu to our app users. So, when we define a function, we use the keyword def, and then we name our function. So, this function is called display menu. This function doesn't require any parameters. We'll talk about that in a moment for our different function. And then we have the colon here. And everything under the colon is going to be what this function will do. So, when we If you've ever seen a menu in Grubhub or Uber Eats or anything like that, at the very top of the menu, you usually see menu. So, we're going to print menu. And this is a new line character, meaning it will add a entire blank line. And this is going to help keep our application a little clean to separate the menu text from anything before or anything after it. The asterisks that I'm using here, just some formatting to make our menu header stand out. And actually, if we wanted to, I'm going to create a new cell under this. And if we run this function right now, we're calling the function with our parentheses. I'm going to press shift enter as a shortcut here. Shift enter, we can see it ran with the number two. Shift enter. And now we see in our output it prints menu. So, obviously this isn't displaying any of our food items yet. So, we need to do that next. So, for every item in our menu, and when I say it that way, that's our clue that we're going to be using a for loop. So, for every item in our menu, and I'm using SKU instead of the word item because item is sometimes a reserved word in Python. And if we use it, it might just give us some funky functionality. So, we're using the word SKU for our loop. And the first thing we want to do is we want to display the number associated with each item in our menu. And what number is menu? So, for example, for brownie, the number associated with it is ID number nine. And for sauce, it's ID number 10. So, to pull that number from the SKU ID, we're going to create a in within the function variable, and slice the SKU, saying everything after that third character. We're going to call that the parsed SKU. So, what else do we need to display for every item in our menu? Well, we need to display the name of the item. And this is where our dictionary work is going to come into play. The item we're using is from our menu dictionary. Looking at the first set of — [snorts] — dictionary key with that nested dictionary value. So, for example, if we're looking at this, we're going to then pull the name. And so, it's going to pull the name for every single SKU as we loop through. Okay, so we're displaying the item number, name, and the final thing we're going to do is display the item price. You can check for yourself right now, if this is how we can display the name, how can we display the price? It's going to be almost identical code to that line before it. The difference being, we're going to pull the price instead of the name. So, now we have all three things that we want to display. But, if we try running this right now, we still don't see anything. We still only see that menu with the asterisks

Building a display menu function

because we haven't printed any of this information. So, this line is going to look kind of crazy, but let's talk through it piece by piece. What we're going to do is we are going to print, and then we have some string concatenation logic where we're adding lots of pieces together. So, to display the item number, I am deciding to put it inside a set of parentheses, and use that number inside the set. Then, we are going to display the item. And then, we're going to display a dollar sign, cuz I'm in the US, I'm using US dollars. You can use whatever currency symbol you want. And then the price. Here's the interesting thing about price. Remember, our original dictionary stores prices as floats. So, we do need to be mindful and convert it into a string when we display it. So, let's see what happens when we run this now. Shift enter is my shortcut for running this cell. And then shift enter to see what happens. And voila, we have our entire menu gets displayed now when we call display menu. So, that's our first function puzzle piece done. But, when a user interacts with our food ordering app, they need to be able to see the menu and actually add things from that menu into their shopping cart. So, that's going to be our next component is how can we have users add things to a shopping cart? So, in a new cell, I'm going to begin this function definition. So, we're defining a function. I'm calling it add to cart. And we are going to have some parameters for this function. This means that in order for the function to activate, it needs to have a little bit of information passed into it. The first thing is what are we adding to the cart? We are adding a food item. And when we're adding a food item, we want to refer to it by its ID number. So, we are going to need that ID number, which is going to be an SKU. The next thing we're going to need when we're adding an item to the cart is how many are we adding to the cart? And to do this, we are going to use a quantity. But, to make our app have a little bit more ease of use, we're going to give quantity a default value. Meaning that if a user doesn't specify how much of something they want, we're going to assume they want one of that thing. If they specify they want more of that thing, then this value can change. Okay, so now let's get into the nitty-gritties of what this function can do. So, the first thing we're going to do is what happens if a user tries to give a ID for a food item that doesn't exist? That's a problem, because it could break our app. So, we're going to check the ID is actually in our menu. And if it's not in our menu, we're going to give some information to display to the user saying, "I'm sorry, that menu number isn't in our menu. " And basically prompt them to try again. This else logic helps ensure that our app doesn't break. So, now that we have some fail-safe logic, let's say what's going to happen if it is a valid ID number. So, if it's a valid ID number, we want to interact with our cart global variable. And remember, our cart global variable begins as an empty dictionary. So, what happens if the cart already has the food item in it, versus what happens if the cart doesn't have the food item in it? So, we're going to nest some if logic here and say, "Whoop, that's not the right thing. " And say, "If the item if the ID for that item is already in the cart, we're going to increment the quantity of that item. " So, looking at our cart dictionary, we have our ID. So, this is saying for this key-value pair, increment by the quantity specified in the function call here. But, if it's not already in the cart, we don't need to increment because it's not adding to anything that already exists. So, in this case, it's going to be very similar code to this line. The difference is that since we're not incrementing it's going to be just a assignment operator which is one equal sign. And regardless of whether we're incrementing or assigning, we do want to display some information to our users. This is kind of like a user interface experience in our text-based app here. So we're going to say print added quantity of and then we're going to display the item name to the cart. And this is our add to cart function. And as you work through any Python or any coding project, I highly encourage you to

Writing an add-to-cart function

test as you go. So for example, let's test out our function right now. But last time we tested our display menu function, we did this. So let's see what happens if we try to do this. We get an error. And what's the error? We are missing one required positional argument. That's right. We need to specify the item number or the item ID. So let's say SKU1 and then see what happens. And we can see the text added one of hamburger to the cart. The grammar is a little funky here, but that's okay for this development phase of the application. I am noticing though that we have some spacing issues because in our print statement, we have some extra spaces that overshot the spacing in the output. So I'm going to iterate here and remove all of those extra spaces and see if that cleans up our output here. There we go. That looks a lot nicer. Um and by the way why didn't we have to specify quantity in our arguments here? Because it has the default value of one. But if we wanted to specify 10, we can do that this way. And now it says added 10 of hamburger to the cart. So our next building block is created. So if you can add items to the cart you also need to be able to remove items from the cart. So our next function will be removing items from the cart. And for this function uh I'm not specifying a quantity. We are just going to say if we're removing an item, we are removing all of that item. For changing the number of things, we're going to have a different function for that. So to remove an item from the cart we first need to check is that item in the cart? So if the item is in the cart what do we want to do? We have a really cool method here. If you haven't seen pop before, it is fun to use because it does two things at once. It looks at a dictionary and it removes and stores the removed value. So it's going to take that item number remove it from the cart and store it into removed value. It's wild that I can do two things at once. I really love that. And then we're going to display to our user that we have removed that value. And if the item isn't in the cart, well, we don't need to do anything because there's nothing to remove. So oh, I had a little bit of formatting with my print uh my else block. So else and then we print I'm sorry the item with SKU na na is not currently in the cart. Um Sam in chat this is a webinar from Dataquest and all of the things that are being shared in the chat from my Dataquest counterpart will take you directly to this project and you can sign up for it there. So one thing I want to point out about these print statements is they are using F strings. And that means formatted strings. That's what this F stands for. And I love printing with F strings because I think it's a lot cleaner. Where we can refer to variables not having to use like commas, addition signs to piece lots of different things together in a print statement, we can put it all within one string and anything that isn't supposed to be string text putting it in curly brackets will pull in that information. So this is our removing from cart function. Let's test it really quickly with the same uh SKU from hamburgers cuz there should be hamburgers in our cart now. And we'll say SKU one. Oh. What did I do? Ah. Well, this is interesting. Do we need to just say one like this? Let's print our shopping cart. This is intriguing to me. Okay, so in Jupiter you can render the last line of a cell without needing a print statement. So if we type in cart into a new line here, we see that the cart is empty. So I think that something I did meant that when we actually added to the cart it was struggling a little bit there. So if we do this and I'm adding more items to the cart now our cart does have 10 hamburgers in it. So this should have worked. What if I just remove this from now?

Writing a remove-from-cart function

Some live debugging here because here's the thing, it is actually removing the item from the cart. So I think the problem it's having is with this removed val um naming thing. So running that again okay, that's fine. Let's add our hamburgers back to the cart. We can confirm hamburgers are back in the cart and now okay. So for now, if you're following along with my code, I'm not sure exactly what happened here. Um but I am just changing the print value to removed val instead of removed val with that name key getting called. If you have questions for me, if you can use the Q& A panel, I will have time at the end of our webinar session today in about 20 minutes or so to go through all of those questions. So some live debugging for everybody. Um All right, so we don't have anything in our cart. We are good to go. So our next function is going to address the final kind of cart interaction we have. So we can add items to the cart, we can remove items from the cart, and now we're going to modify the cart. So let's say you accidentally added 100 hamburgers, but you only meant to add one. The modify cart function is going to be how we can do that. And because it's modifying the quantity we're going to need two parameters. the item ID as well as that quantity. Notice that we are not specifying the quantity here because we're not sure how many people want to add or remove. But I am actually going to give you some homework. Because walk-throughs are great but hands-on practice is the best best way to learn a new skill. So I'm going to add some placeholder text and say print this is a placeholder. And the reason I'm doing this is just so that we have the function. So when you do take this type of code and work on it on your own this not having this won't break our code later on. But the reason I'm setting this as homework is because the logic for modifying the cart is very very similar to the logic we've already done for adding and removing from the cart. There's a couple nuances to add to it. But I think this is a great test of your knowledge. So we're going to leave that there. I'm going to store that into my Jupyter lab environment. And the next thing that we're going to do is getting a viewing of the cart function. Because if you add a bunch of items to your cart, we called the cart manually like this, but that's not a very nice user experience. So, that's why we're going to specify a view cart function. But viewing the cart doesn't require any additional input from the user, so we don't need any optional parameters or any required parameters. And when you view a shopping cart, you usually have some kind of header at the top. So, we're going to say cart contents. Excuse me. And once again, we're using new line characters to have clean white break lines between Excuse me. I'm sorry if you had a pop there. I hit my microphone. And just have a little bit of separation between our heading here. So, when you view a shopping cart, you will see the total amount of money that you're currently investing into this shopping experience. So, we're going to initialize a subtotal that is currently set to $0. And then for every item in the cart, we're going to add to this subtotal. And so, for every item in our cart, the first thing we want to check is adding some fail-safe logic where is that item actually in the menu? Did a user somehow add something to their cart they weren't allowed to or weren't supposed to? Like item number 15 that doesn't exist in our menu dictionary. That would break all of the logic. So, we just want to double-triple check that the item actually exists in the menu. And then, if the item is in the menu, we first need to know the quantity that is in the cart. And to pull the quantity, we're looking at the cart. And our cart stores just the ID number and the quantity. It doesn't need the name and price information because we can pull that from the menu itself. The next thing we're going to do is I'm going to write this line by line manually is we're going to increment the subtotal. And to increment the subtotal, we're using our increment operator. But to pull the price, this is where we need to reference the menu information. So, looking at the menu, we look at the ID. And so now we're in that nested dictionary. If you tuned in a little late, here's the structure. So, for every ID in our dictionary, there's a nested dictionary with a name and a price. And we're going to want to pull the price. And so, that is why we have this double bracket situation going on here. And we also need to multiply this by the quantity that exists in the actual shopping cart.

Modifying cart items with an update function

Then, we're going to print for our user what exists in their shopping cart. So, this print statement shows the quantity times the menu name. So, this will be something like uh five x ham burger, like that. The next thing that we need to do to show our users the total amount of money they're going to spend is the amount of tax that's being charged to the order. But we don't need to show tax for every item in the cart. Tax is applied just once. So, we're going to remove ourselves from any loops and be just in the main function body now and say that the amount of tax will be the subtotal times our global variable of the sales tax rate. Then, the total that our users will owe for their food order experience will be the subtotal plus that tax amount. And finally, for a little bit of a user experience or user interface experience, we're going to print total with the dollar amount. And here we're going to round the total to two decimal places because US dollars are only um

Displaying the cart with formatted output

done in two decimal places. If your local currency is different, you can change this if you want to. And this will show our cart contents. This is the completed function. So, let's think, what other building blocks do we need to have an entire food ordering experience? The final thing I can think of is to actually click check out. Uh so, if you've ever used one of these apps before, you see your cart, and if you're happy with the items, then you click a button, voila, you're checking out. And because of that particular workflow, we don't need any additional parameters here. It's just going to say check out. And we don't have any point of sale or actual charging mechanisms to add here. That would be a next step if you did want to implement something like this for a real business. But we are just going to display the cart to the user. And then, we're going to thank them for their order. So, a very quick, simple little function compared to the others we've been writing today. And this is our checkout function. All right. Now, we are going to get to, in my opinion, the most challenging bit of code we're going to write today. So, stick with me. I'm going to do my best to explain it, but of course, if you have any questions for me, I would love to hear them in the Q& A, and we will definitely get to them. So, this function is going to be the piece that gets user input. And when we get user input, what are the types of input that we need to get from a user? the SKU, and sometimes quantity. So, for our parameters, we are going to give an SKU prompt. And sometimes, we're going to have a quantity prompt, but not every time. So, we have a default value here of none, meaning that in our functions that don't require a quantity, we can keep this as none. But in functions that do require a quantity, we can specify that. So, this keeps this function flexible. So, we are going to use a really cool built-in Python function called input. This is native to Python. You don't need to install anything or do anything funky. Do note though that if you're working with the Dataquest environment, input will only work in the Jupyter Lab environment um because you need to have a way for a user to actually do the input part. Uh so, it'll work for our case here. So, we're going to input or get input from the user with our SKU prompt. And that is going to be assigned to a variable named item SKU. So, this is the item ID number. But we don't want a user to have to type out SKU eight. That's not a good user experience. So, we just want a user to have to type eight, for example. But we know that our ID numbers have that SKU in front of them. So, we are going to say or reassign item SKU to be SKU plus the item SKU. Um and this brings up an important point that the input function will create a string. So, item SKU is stored as a string here. So, the next thing we're going to do is if quantity prompt exists. So, basically, if it's not none, then this little bit of logic is going to run. And if quantity exists, we're going to ask the user for quantity input. How many hamburgers do you want? They're going to type in the number. But we also need to check, what if a user, instead of typing in 10 for the number of hamburgers, what if they type in um 10? Or some kind of nonsense? We need to account for that. So, we're going to check if quantity And then, this method is going to check

Building a checkout and total calculation function

for the type of value we have. So, if it's not a digit, then we're going to give a default quantity for them. We're going to say, okay, if they typed in something funky, we're going to say the quantity is one. Keeping it simple here. But, if this doesn't have to run because the user did in fact type in a digit, we're going to say that quantity is equal to the integer version of quantity. And remember, we have to do this because input stores the original input value as a string, so we have to convert that type. Then, so far we haven't had to return any values in the functions we've written, but this is going to be our first return statement, and it's a cool one because we're returning two things at once. We are returning the item SKU and the quantity, and we can do this because it's still within the quantity prompt conditional logic. This will return a tuple, that's t u p l

Handling user input

e, and a tuple will be similar to a list in that it has multiple things in it separated by a comma. And then we're going to later unpack that tuple so we have both items in the return statement available to us. Now, if there is no quantity prompt, oops. Then, if there's a quantity prompt, then we return this. If there's not a quantity prompt, we only return the item SKU. And now this function is done. Congratulations if you stuck with me through that. I think that this is a great point to test your own knowledge on and see, can you walk through this bit of code on your own? Um cuz I do think that there's a lot happening here. All right. So, we have done all of the building block pieces. Now we get to create our actual app ordering loop. So, this is going to be what the user interacts with in order to add items to the cart, in order to remove items from check out. So, we're going to define our order loop. No parameters are needed. And we are going to display to our user, welcome to and then whatever our restaurant name is. We're going to use a while loop because if you've done any kind of programming before, you'll know that you click run and the Python runs and then it's over. But we don't want the experience for the user to be over. We want it to continue until they are finished with their food ordering process. So, a while loop is the best way to do that. Oops. But while loops always need a stop condition, otherwise they'll run on forever and ever and ever. So, we are going to say, when this function is running, ordering is going to be true because that's what our user is doing. And while the user is ordering, we're going to do some logic. If ordering is ever false, the while loop will stop, the program will end. So, while a customer is ordering, the first thing we want to do is display some ordering actions. And this is just a little bit of a user interface experience so that they know what's happening. Then, for every number in app actions, and app actions, as a reminder, was our global level variable that gave numbers for actions. So, for every number in the app actions, we're going to say that the description is the value from that dictionary, and we're going to print the number of the app action as well as the description. Then, after we've displayed the ordering actions, we need to get the response from the user. For example, if they want to press a number from the app actions, maybe they want to add an item to their cart, then we're going to get input that says, please enter the number of the action you want to take and store that into a variable named response. Then, if response equals one, well, one corresponds with adding a new item to our cart, and this is the most exciting part of the entire project in my opinion because all of our building blocks come together. Because if they're adding an item, the first thing we want to do is show them the menu so they know what items are available. Then, when they're adding an item, they need to give us two pieces of information. The number of the item they want and how many they want. And because we have two things, we're going to have an SKU prompt to get the ID of that item number and a quantity prompt to say how many of that item they want. And then, we're going to get the user input for this information using our get user input function defined in the previous cell. And remember how we did some tuple work in our return statement? Here we are going to unpack that tuple and assign two variables at once with a comma. And the reason this works is our return statement, where'd it go? Uh our return statement shows two items or two variables. And then, now that we have the ordered SKU and the quantity, we can take those variables and pop them into our add to cart function. Isn't that cool? So, that's how a user can add an item to a cart. But what if the response is two? Two corresponds with removing an item from

Creating the main ordering loop

the cart. Now, I'm going to move a little more quickly here. And the first thing we want to do if they're removing an item is display the menu so they know what food item corresponds with what number because we are going to prompt them about removing a specific number. And then, we're going to get user input based on that item number and call remove from cart. The next one is going to be what happens if a user responds with three. And three is going to be for modifying an item from the cart. And this one This is the one I said was some homework for you. So, instead of displaying the menu, let's just call modify cart, and that will show our placeholder text for the time being. So, when you do complete your homework, you're going to want to add a little bit more to this particular part of the ordering loop. So, now what happens if a user presses four? Four corresponds with viewing the cart. Well, this one's a very simple because we've already been doing it in our um ac- actually, it's very simple cuz viewing the cart doesn't require any user input. We just called a viewing cart function. So, the next one is number five, and this is if a user wants to check out. So, if they want to check out, our checkout function does not require any user input, so we can just simply call checkout. But there's a very important thing we need to do here. Is if they've checked out, they're no longer ordering. The order is complete, so our loop variable for ordering needs to get set to false. Then, our final valid response number is six, and this is if a user wants to exit the program. And if they are exiting the program, we're going to let them know we understand and say goodbye. But if they're exiting, they are no longer ordering, so once again, we're going to say ordering equals false. So, we could end this here, but programming best practice indicates that we need a condition for what happens if a user responds with a number that doesn't actually correspond with anything valid. So, we're going to have else logic. So, if they respond with anything else, we're going to print, you've entered an invalid action number, please try again, then the loop will continue. All right, and congratulations, we have officially finished all of the code for this project. But the most fun part is to test it. So, I'm going to save this into our environment so we know everything's working, and just because I know that we were developing along the way, I'm going to press the double arrows to restart all the code from the top, clean the environment out. And once this renders, the asterisks shows that it's like in progress. All right, it's rendered and now we're going to test our loop. So, to test our loop, we're going to call the order loop function. To call a function, you must include parentheses. If you don't, the function is just an object and it's not going to do anything yet. So, shift enter to run this and here's where the magic happens. Look at this. Immediately, we're seeing app actions. The asterisk is showing because this cell is continuing to run because we are being prompted for input. So, it says, "Please enter the number of the action you want to take. " Well, let's add a new menu item to our cart. So, I'm going to select number one. And it says, "Please enter the SKU number for the menu item you want to order. " Well, I've been talking about hamburgers this entire webinar, so let's say I'm going to order some hamburgers. And enter the quantity you want to order. Well, I'm going to say 10. I'm hungry, okay? So, we just added 10 hamburgers to the cart. And let's add another one. I want to add Let's add some fries. So, that's number four. And we'll add 10 fries to go with our 10 hamburgers. What happens if we view the cart now? So, view cart is number four. And we can see, look at this. We see our cart contents. And now, I want to just test all the actions we have. So, let's remove an item from the cart. And I'm going to remove those french cut fries cuz $95 for hamburgers and fries, that's a little expensive for me. So, french fries number four, I'm going to remove that. Whoop. Uh-oh. What I do? Oh, no. Oh, I messed up. I turned this into a markdown cell instead of a code cell. So, I broke it. We'll have to start over. Oh, due to pending input, but I can't do pending input because it turned into markdown. All right, we're just going to restart this kernel again, clean it out. There we go. Okay, so very quickly, let's add those hamburgers back to our menu. My apologies for that. Okay, so we added 10 of hamburger to the cart. Let's add those french fries back in. So, french fries was number four. We're adding in 10 french fries. And now we're going to remove our french fries. And we are Okay, we removed 10 french fries from the cart. And now, I want to show you what happens if we do go to the modify function. Oh, dang it, I did it again. How am I doing that? My apologies, everybody. Okay, let's see what happens if we click modify cart. Here we see that this is a placeholder text. So, this is something that we could spend a lot more time on just testing out, okay, what if a user does this um order of events. Uh what if events? But, the last thing I want to test is the checkout function. So, checkout is number five. And our checkout, we see our cart contents and then we see thanks for you for our order, goodbye. So, sorry for the little bit of confusion there with um the markdown issue. That was just me typing incorrectly. You shouldn't have that issue um when you test your own code out. But, that brings us to the end of our project code. I have some next steps for you though. So, if you want to take this project further, and I always encourage you to do so, add extra menu items and prices. Modify the app actions that users can take. Um for example, maybe you have a functionality that collects a user review or something like that. Um there are also some areas in

Testing the complete food ordering app

the code that could be refined further to reduce repetition because in code, there is a saying called don't repeat yourself. So, if you are finding you're typing the same thing over and over again, that could be an area to reduce the repeatability. Um and then, another great function to add is what if you want to clear all contents from the cart? That'd be a great one to add in. So, at this point in time, I am going to go through the Q& A. Go ahead and submit your questions. We have about 10 minutes to get through these questions. If you do need to hop off, I completely understand. Want to thank you for joining. But, let's go ahead and look at those questions now. Okay. Of Las is asking, "Is integration with delivery and payment going to be covered or is this a standalone app by itself? " So, this is kind of demo app logic where you can see we don't actually have a user interface. This is pretty bare-bones, but could be interesting to add it to um some kind of user interface experience. Um if you wanted to play around with this, maybe something like Streamlit, you could create a fun little app. It wouldn't actually collect payment for my user because co- connecting payment options is a whole other can of worms that goes a little well, it goes a lot beyond the scope of this cuz you have to worry about security and all of that. But, for a very beginner-friendly project, this is the foundations that all of those more complex topics still are rooted in. Uh and you're trying to create exactly a pastry shop app for multiple stores. So, if you are working with app development um like for a phone or something, that's iPhone. Um you'll need to work specifically with like a phone application type of programming. I don't know if you can connect Python to that or not. I did a little bit of work with Kotlin to work on Android Studio app work and um it it's just a separate thing. Um that has its own user interface options and all of that. But, the type of functions we wrote here, it's going to translate. And how do you utilize cookies for the cart? Um I'm not sure if you're talking about cookies like this or a computer cookies. And if it's cookies like this, you'd press number eight. For computers [snorts] cookies, that's a little that's a lot beyond the scope of what we're talking about today. And do I think that using R would make this app more compact? Using Shiny helps work through back end and front end all in one page. Um I am not very familiar with R, so I can't speak to that. Um unfortunately. Um And Kingsley says, "Without UI, it seems I've done nothing. " Well, it's all about front end versus back end. And if you've ever heard those phrases before, front end developer, back end developer, full stack developer, this is the type of work that your back end developer would do. So, it's not um nothing. Front end developers are the ones who then takes this and creates the thing the user will see. So, we were acting as the back end developers for this app today. Um and Priscilla asks, "You created a similar app, awesome, but it was a command line app. Is this going to be a command line application? " So, if you run this code in um any kind of IDE, you could run it in your terminal terminal, which would, you know, be your command line. Um and the functionality would be the same. Um So, it's just the way that Jupiter renders the input function from Python. It renders it within those output cells, but but yeah, you could completely do this same workflow within a command line. All right, looking at our Q& A box. Um And Alu wanted to has a great question. Is it not better to see what is in the cart at the point of deletion? That's a great addition. I think that when something is removed from the cart, displaying the actual cart contents would be uh something simple to add to the remove cart function that would add a lot of user value.

Audience Q&A

So, how would you do that? You would probably just um call in the view cart function after this so that users could see what's in there. Um and then also is the difference in using int input message versus taking the input first and then checking if it's a digit. Um it's just the way that we checked is digit is more flexible because here's I'm going to copy and paste so people can see the logic we are discussing. So, this is what we did. The question is about something like this where you're using input with int here. Um because we're asking for input and then checking for a digit, this allows us to not have everything break all at once if the input is incorrect. So, this is more rigid. Um but if you account for that rigidity somehow else like including this within a try accept block, then that would be fine. That would be equivalent. All right, and wonder okay, I can look at chat now because I did go through all the Q& A. So, let me catch up with you guys in the chat here. Supriya asks, uh can we send questions via email after this webinar? You're asking cuz you missed the beginning and need to watch the recording. So, on YouTube, I love checking and responding to YouTube comments and that's I will put this recording on YouTube today or tomorrow so you can ask me on the YouTube comments. Additionally, if you go into the Dataquest community and if you go from this project link, it will take you directly to a place where you can ask about this project. And if you're in the Dataquest community and you tag me, tag me. So, if you tag me at Anna _straw, I would love to answer questions there. Additionally, if you tag me with your finished project, I'll take a look and potentially offer some feedback on it. So, either YouTube or Dataquest community is the way to go there. And Stephanie says there's value in learning the logic and syntax here, especially if you're a beginner. Yes, this project is geared at beginners. Meaning, there are some concessions we had to make um for like production level design. This is not production ready at all. But, this project appears in our intro Python content. So, for people who are just beginning out with Python, you got to do some pretty cool work here. And so, wonder, this might kind of go along with your question. How do you create devops for this project? You would like to make something professional from this. I am not very well versed in devops at this point in my career. Um so, I can't speak to it. However, um if you're wanting to go into devops, something like Django um might be useful if you're looking to stay in the world of Python. Um maybe you could consider something Flask related. Yeah, that's where I would start. And Tanico, so you will act as a front end developer later for this application in another session. Like I said, no work we're finishing the food ordering app here. However, if you do want to do the front end design, if you're brand new to front end design, something like Gradio, I'll type that here for you in a markdown cell. Gradio is a very easy front end design or Streamlit. I like using Streamlit. It's not going to be a big flashy perfectly rendered application interface for this type of thing, but if you're looking to share some kind of user interface with the public, these are both options that can help you do that. So, at this point in time, I just want to thank you all so much for attending the webinar. I hope that you learned something new and I wish you all the happiest of coding. Thanks, everyone.

Другие видео автора — Dataquest

Ctrl+V

Экстракт Знаний в Telegram

Экстракты и дистилляты из лучших YouTube-каналов — сразу после публикации.

Подписаться

Дайджест Экстрактов

Лучшие методички за неделю — каждый понедельник