Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why does my Python code only add up the price of the last item in my shopping cart?

So I am trying to make a python code that lets you add items to your shopping cart then once you type "done" it is supposed to add up the prices of each item you added to your shopping cart and then tell you the total price.

My issue is that once I type "done" it only adds up the price of the last item that I added to my shopping cart then prints it out.

shopping_cart = [""]

food = shopping_cart

#Stock
stock = {
    "\nFruit\n\n"
    "banana": 50,
    "apple": 15,
    "orange": 32,
    "pear": 15,

    "\nDrinks\n\n"
    "apple juice": 10,
    "orange juice": 10,
    "pinapple juice": 10,
    "milk": 15,

    "\nSnacks\n\n"
    "chips": 20,
    "cookies": 20,
    "chocolate": 25,
    "lollies": 50
}

#Prices 
prices = {
    #Fruit
    "banana": 10,
    "apple": 5,
    "orange": 5,
    "pear": 5,

    #Drinks
    "apple juice": 5,
    "orange juice": 5,
    "pinapple juice": 5,
    "milk": 4.80,

    #Snacks
    "chips": 7,
    "cookies": 4,
    "chocolate": 6,
    "lollies": 5.50
    
}

#Loop
while True:
    item = input("\nEnter an item: ").lower()
    shopping_cart.append(item)

    if item == 'done':
        break

    if item == 'cart':
        print(shopping_cart)

    if item == 'stock':
        for k, v in stock.items():
            print(k + ":", v)


#Define the bill variable
def shopping_bill(food):
    total = 0
    for key in prices:
        for key in stock:
            for item in food:
                if item == key in prices:
                    total += prices[key]
        break 
    print("Your bill total equals $", total)
    print("Thank you for shopping at B&K Supermarket!")

shopping_bill(food)

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Mostly the same as the other answer but checks that the stock doesn’t go below 0.

shopping_cart = []

food = shopping_cart

#Stock
stock = {
    #Fruit
    "banana": 50,
    "apple": 15,
    "orange": 32,
    "pear": 15,

    #Drinks
    "apple juice": 10,
    "orange juice": 10,
    "pinapple juice": 10,
    "milk": 15,

    #Snacks
    "chips": 20,
    "cookies": 20,
    "chocolate": 25,
    "lollies": 50
}

#Prices 
prices = {
    #Fruit
    "banana": 10,
    "apple": 5,
    "orange": 5,
    "pear": 5,

    #Drinks
    "apple juice": 5,
    "orange juice": 5,
    "pinapple juice": 5,
    "milk": 4.80,

    #Snacks
    "chips": 7,
    "cookies": 4,
    "chocolate": 6,
    "lollies": 5.50
    
}

#Define the bill variable
def shopping_bill(food):
    total = 0
    for key in stock:
       total += food.count(key) * prices[key]
    print("Your bill total equals $", total)
    print("Thank you for shopping at B&K Supermarket!")

#Loop
while True:
    item = input("\nEnter an item: ").lower()


    if item == 'done':
        shopping_bill(shopping_cart)
        break

    if item == 'cart':
        print(shopping_cart)
        next

    if item == 'stock':
        for k, v in stock.items():
            print(k + ":", v)
        next

    for key in stock:
       if item == key:
              if stock[item] > 0:
                     shopping_cart.append(item)  
                     stock[item] -= 1
                     print("added to cart")
              else:
                     print("out of stock") 
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading