Python shopping cart program πŸ›’

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everybody today we will be creating a shopping cart program this program will be an exercise to follow the previous lesson on lists sets and tuples the more that we practice with those collections the better we'll be at using them so I thought we'd create an exercise to get the hang of it before moving on in this program we will have two lists Foods these lists will be empty we'll declare them but not use them quite yet and prices then a total total equals zero the reason that I'm not using tuples is that tuples are unchangeable we're going to ask a user what food they would like to buy we can't depend any elements to a tuple we're not using sets because sets are unordered I mean I guess technically you could but at the end of this program I'm going to print our shopping cart in order so I think lists would probably be the best we have an empty list of foods and an empty list of prices we'll use a while loop while true if our condition is set to True we'll need some way to break out of the while loop we'll need a break statement somewhere we'll get to that later we will ask the user what food would they like to buy let's declare variable food equal to input enter a food to buy to exit the while loop you need to press q q 2 Quit then let's check if food is equal to Q lowercase Q then we will break we're not done with the program but let's at least test it Pizza hamburger hot dog cue to quit okay it looks like it works now what if somebody types in uppercase q Pizza hamburger uppercase q well we can't actually quit after accepting our user input if food dot lower method this will take our input make it lower case just for a comparison let's try that again Pizza hamburger I'll type capital Q to quit and that is valid follow food with the lower method to temporarily make the user input lowercase just in case they type in capital Q if the user doesn't want to quit let's add an else statement else let's take our Foods use the append method then add whatever food the user typed in we'll also need a price let's ask a user for the price price equals input enter the price of let's use an F string a whatever food the user types in pick a unit of currency I'll pick dollars we are working with numbers we should Typecast our input as a floating Point number since we're working with prices so we will accept a price add our food item to our list of foods do the same thing with prices prices dot append whatever the price was and that is the while loop Let's test this program again to be sure that everything's working Pizza will be 5.99 enter a food to buy hamburger hamburgers will be 350. hot dog hot dogs will be 175 I would like to quit I will type either capital Q or lowercase Q both will work and we have escaped the while loop so the while loop is now complete outside of the while loop let's display our shopping cart let's print some decorative text maybe five dashes your cart then another five dashes I will then iterate over all of the elements found within my Foods list for every food in my list of foods let's print each food item let's take a look so far again we have a pizza the price was 5.99 hamburger the price was 350. hot dog 175 cue to quit okay your cart that will display the individual list items if you would rather have these list items arranged horizontally in one line you can add this keyword and equals this end keyword will replace the new line character at the end of a print statement with some other character like a space let's try that again I'll try not to take too much time Pizza 5.99 hamburger 350. hot dog 175. you to quit yeah that's much better we are horizontally listing all of the different items within our list you could revert back to the vertical list if you'd prefer that I'll keep my output like this then we will need to iterate and add up all the prices for every price and prices we do have a total variable that we declared let's utilize that total equals total plus price otherwise we could shorten this to plus equals price that would do the same thing then we will display the total print I'll use an fstring your total is I'll add a unit of currency I picked the dollar sign whatever the total is okay let's run this program one last time enter a food to buy Pizza which was 5.99 hamburger which was 350. hot dogs they are 175 Q to quit here's your shopping cart I'm just going to add one new line real quick right before we display the total just an empty print statement here are the results your cart Pizza hamburger hot dog your total is 11.24 cents all right everybody well that is a shopping cart program I thought this would be a fun exercise to follow the previous lesson on lists sets and tuples and well yeah that is a shopping cart program in Python
Info
Channel: Bro Code
Views: 95,460
Rating: undefined out of 5
Keywords: python tutorial for beginners, python course
Id: kbyHLU9JqjE
Channel Id: undefined
Length: 7min 13sec (433 seconds)
Published: Tue Nov 08 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.