coding-pythonx / supermarket-cashier

Subject 14 - Project One: Supermarket Cashier
37 stars 10 forks source link

Bug or not? #1

Open AndriyTonkikh opened 2 years ago

AndriyTonkikh commented 2 years ago

i just copy/pasted ur code and see what isi happened: C:\Users\KateFox777\PycharmProjects\pythonProject\venv\Scripts\python.exe C:/Users/KateFox777/PycharmProjects/pythonProject/main.py Press A to add product and Q to quit: A Enter product: Egg Enter quantity: 54 Press A to add product and Q to quit: A Enter product: Biscuits Enter quantity: 7 Press A to add product and Q to quit: Apple Please select a correct option Press A to add product and Q to quit: A Enter product: Apple Enter quantity: 45 Press A to add product and Q to quit: Q Enter customer membership: Gold Egg:$1x54=54 Traceback (most recent call last): File "C:\Users\KateFox777\PycharmProjects\pythonProject\main.py", line 63, in makeBill(buyingData, membership) File "C:\Users\KateFox777\PycharmProjects\pythonProject\main.py", line 56, in makeBill billAmount += getPrice(key, value) File "C:\Users\KateFox777\PycharmProjects\pythonProject\main.py", line 28, in getPrice subtotal = priceData[product] * quantity KeyError: 'Biscuits'

stiperaketa commented 2 years ago

im geting a simular error

hydr0phobia commented 2 years ago

Well i believe that you have to enter the product name exactly the way it is written in the dictionary, so, in this case it would be -"Biscuit" not "Biscuits". However im getting a different type of confusion here, that is - why is the code saying -"The discounted amount is $",and the value it puts there is the total bill amount and not the dicount amount. How should i change the code to bring out the discount amount? this is my code:

def enterProducts(): buyingData = {} enterDetails = True while enterDetails: details = input("Press A to add products and Q to quit ") if details =="A": product = input("Enter Product: ") quantity = int(input("Enter quantity: ")) buyingData.update({product:quantity}) elif details == "Q": enterDetails = False else: print("Please select a correct option!") return buyingData

def getPrice(product,quantity): priceData = { 'Biscuit':3, 'Chicken':5, 'Egg':1, 'Fish':3, 'Coke':2, 'Bread':2, 'Apple':3, 'Onion':3 } subtotal = priceData[product]*quantity print(product+':$'+str(priceData[product])+'x'+str(quantity)+'='+str(subtotal)) return subtotal

def getDiscount(billAmount, membership):
discount = 0
if billAmount >= 25:
if membership == 'Gold':
billAmount = billAmount 0.80
discount = 20
elif membership == 'Silver':
billAmount = billAmount
0.90
discount = 10
elif membership == 'Bronze':
billAmount = billAmount*0.95
discount = 5
print(str(discount) + "% off for " + membership +
" " + "membership on total amount: $" + str(billAmount)) else:
print("No discount on amount less than $25")
return billAmount

def makeBill(buyingData, membership): billAmount = 0 for key, value in buyingData.items(): billAmount += getPrice(key, value) billAmount = getDiscount(billAmount, membership) print("The discounted amount is $" + str(billAmount))

buyingData=enterProducts() membership = input("Enter customer membership:") makeBill(buyingData, membership)

ReaZon17 commented 2 years ago

hydr0phobia I had the same issue ! The way I solved it was to first assign the variable "discount" a value, then print the message for each membership option, and then do the math for the discounted price. That way it displays what I want ! I am a complete beginner, I don't know of better ways to do it, but I think this works pretty well. So for the getDiscount function I did :

def getDiscount(billAmount, membership):
    discount = 0
    if billAmount >= 25:
        if membership == "Gold":
            discount = 20
            print(str(discount) + "% off for " + membership + " membership on total amount: " + str(billAmount) + "€")
            billAmount = billAmount * 0.80    
    elif membership == "Silver":
        discount = 10
        print(str(discount) + "% off for " + membership + " membership on total amount: " + str(billAmount) + "€")
        billAmount = billAmount * 0.90

    elif membership == "Bronze":
        discount = 5
        print(str(discount) + "% off for " + membership + " membership on total amount: " + str(billAmount) + "€")
        billAmount = billAmount * 0.95    

else:
    print("No discounts on amount less than 25€")
return billAmount

And it displays :

20% off for Gold membership on total amount: 88€ The discounted amount is 70.4€

sivasai7205 commented 1 year ago

The getDiscount function output is not giving us the total amount. That is giving us discounted amount only. As we are displaying the Total amount after we executing complete discount calculations and updating that with billAmount variable.

def getDiscount(billAmount, membership): discount = 0 if billAmount >= 25: if membership == 'Gold': billAmount = billAmount 0.80 discount = 20 elif membership == 'Silver': billAmount = billAmount0.90 discount = 10 elif membership == 'Bronze': billAmount = billAmount*0.95 discount = 5 print(str(discount) + "% off for " + membership + " " + "membership on total amount: $" + str(billAmount)) else: print("No discount on amount less than $25") return billAmount

Thank you ReaZon17. Your suggestion helped me getting exact output.