In this post, we create shopping cart program where we can add items to list,total their prices and keep track of quantity of each items.

Shopping cart in a supermarket Stock Photo by ©ginasanders 24701081

How this program works?

We will use list data structure to add items in shopping cart. We are going to ask user what food they would like to buy. At the end of this program i am going to print our shopping cart. Initially we will have empty list of items and empty list of item prices.

cart_items = []
items_prices = []
cart_total = 0

We will use while loop and set our condition to True and we will need some way to break out of the while loop we will need a break statement somewhere. We will ask user which items would they like to buy , for this, input statement is needed which will collect user data through keyboard and store it in declared variable. To exit the while loop,user need to press ‘q’ to quit. then we will test the condition if user enter lowercase letter q then we will break. Now what if somebody types in uppercase Q , we cant actually quit, after accepting our user input, lower() function will convert that input into lowercase letter.

while True :
    itemName = input("Enter a item name to buy (q to quit) : ")
    if itemName.lower() == 'q':
        break

If the user doesn’t want to quit lets add an else statement. else lets take our item and use append method then add whatever item the user want. We will also need a price , lets ask user to enter price using input statement and pick a unit of currency here i will pick $ . We are working with numbers,we should typecast our input as a floating point number. Since we are working with prices so we will accept a price and add our item to our list of items and we will do same for prices.

else :
        itemPrice = float(input("Enter the price of a {itemName}: $"))
        cart_items.append(itemName)
        items_prices.append(itemPrice)
    

So the while loop is now complete. Lets display our shopping cart and print some decorative text using some special symbols . i will use + symbol to decorate and then i will iterate over all of the elements found within my items list ,for this i will use for loop to iterate the elements in items list and print each cart item list using print statement. print statement will display individual items if you would rather have these items arranged horizontally in one line, you can add end= , end keyword will replace the new line character at the end of print statement with some other character like space.

print("++++++++++++Your Shopping cart ++++++++++++")
for itemName in cart_items :
    print(itemName,end=" ")

Now we will need to iterate and add up all the item prices, for loop will be needed to do this. We do have a total variable that we declared lets utilize that total variable to add item prices. we will display total item price i will use fstring and i will add a unit of currency $ sign whatever the total is.

for itemPrice in items_prices:
    cart_total = cart_total + itemPrice
print()
print(f"your total is : ${cart_total} ")

Here are the results of your shopping cart

Here is complete python shopping cart program source code

<

Leave a Reply

Your email address will not be published.