how to find total price

Viewed 287

I don't know how to get the all total price of the product that the customer bought from my system. the price of each item I can get but the price of all the item that have been choose i can't get the output. I'm using python.

Below is my code:

#below is the price of products

shopping_cart = {}
option = 1

while option!= 0:
    option = int(input("What type of milk you want to purchased: "))

    if option == 1:
       print("coke")
       qnty = int(input("Enter the quantity: "))
       total = qnty*8.49
    elif option == 2:
         print("dutch lady")
         qnty = int(input("Enter the quantity: "))
         total = qnty*8.50
    elif option == 3:
         print("fanta")
         total = qnty*3.00
    elif option == 4:
         print("Yoghurt")
         qnty = int(input("Enter the quantity: "))
         total = qnty*16.50
    elif option == 5:
         print("pepsi")
         qnty = int(input("Enter the quantity: "))
         total = qnty*18.50
    elif option == 6:
         print("beer")
         qnty = int(input("Enter the quantity: "))
         total = qnty*20.70
    else:
        ("Not found")

def totalcart (pr,qnty):       #is my code correct here?
    totalcart = (pr * qny)
    return total
print("The total price in your cart is ", (total))
break
print("Come Again")
3 Answers

Just putting ("Not found") as a statement doesn't print anything. You need to add your item total into total_cost immediately after the print statement. There's no point defining a function, since you don't call the function. So, replace everything after that last "else:" with this:

    else:
        total = 0
        print("Not found")
    total_cost += total

print("The total price in your cart is RM ", total_cost)
print("Please Come Again")

You can add total_cost += total for every choice, So its added to the total cost when somebody choose the item and if he add another it also adds to the total cost.

elif option == 6:
         print("Foaming Milk")
         qnty = int(input("Enter the quantity: "))
         total = qnty*12.70
         print("The price is: " + str(total)) 
         total_cost += total
  1. You basically have the same couple lines for every option
  2. You dont save your values

You can try this:

items = {
    "UHT Milk" : 6.49, 
    "Pasteurised Milk" : 5.50, 
    "Sterilised Milk" : 4.00, 
    "Eating and Drinking Yoghurt" : 10.50, 
    "Full Cream Milk" : 15.50, 
    "Foaming Milk" :12.70
    }


shopping_cart = {}
total_cost = 0

while True:
    option = int(input("What type of milk you want to purchase: "))
    if option == 0: break

    elif option == 1: milk = "UHT Milk" 
    elif option == 2: milk = "Pasteurised Milk"
    elif option == 3: milk = "Sterilised Milk" 
    elif option == 4: milk = "Eating and Drinking Yoghurt"
    elif option == 5: milk = "Full Cream Milk" 
    elif option == 6: milk = "Foaming Milk"
    else:
        print("Not found")
        continue

    print(milk)
    qnty = int(input("Enter the quantity: "))
    shopping_cart[milk] = qnty

    total = qnty*items[milk]
    total_cost += total
    print("The price for this product(s) is: " + str(total))
    print('The total price is: %.2f' % total_cost)
    
    
print("\nThe total price in your cart is %.2f " % total_cost)
print("Please Come Again")

also you already had the dictionary with names and prices but didnt use it. just get the price from the items dictionary, but make sure that the name is the same as the option or else you get a KeyError.

Since Python 3.10 you could also use match case if you like that a bit more. just replace all elif's with this:

match option:
    case 1: milk = "UHT Milk" 
    case 2: milk = "Pasteurised Milk"
    case 3: milk = "Sterilised Milk" 
    case 4: milk = "Eating and Drinking Yoghurt"
    case 5: milk = "Full Cream Milk" 
    case 6: milk = "Foaming Milk"
    case _: 
        print("Not found")
        continue
Related