₦airaland Forum

Welcome, Guest: RegisterLoginWith GoogleTrendingRecentNew

Stats: 3,325,102 members, 8,420,346 topics. Date: Thursday, 04 June 2026 at 05:08 PM

Toggle theme

Please Help With My Python Shopping Cart Script - Programming - Nairaland

Nairaland ForumScience/TechnologyProgrammingPlease Help With My Python Shopping Cart Script (2452 Views)

1 Reply (Go Down)

Please Help With My Python Shopping Cart Script by shady1234(op):
I applied for the Andela fellowship and have completed all quizzes and labs except this shopping cart lab. My solution has passed all visible tests but I don't know why it's failing to pass the hidden tests. Can anybody tell me what's wrong with my code or what hidden test they think I'm failing? Below is my code


class ShoppingCart(object):
def __init__(self):
self.total = 0
self.items = dict()
def add_item(self, item_name, quantity, price):
if item_name and quantity and price:
if type(item_name) == str and type(quantity) == int and type(price) == int:
if item_name in self.items:
self.items[item_name] += quantity
else:
self.items[item_name] = quantity
self.total += quantity * price
def remove_item(self, item_name, quantity, price):
if item_name and quantity and price:
if type(item_name) == str and type(quantity) == int and type(price) == int:
if item_name in self.items:
if quantity > self.items[item_name]:
del self.items[item_name]
else:
self.items[item_name] -= quantity
self.total -= quantity * price
def checkout(self, cash_paid):
if cash_paid and type(cash_paid) == int:
if self.total > cash_paid:
return "Cash paid not enough"
else:
return cash_paid - self.total
class Shop(ShoppingCart):
def __init__(self):
self.quantity = 100
def remove_item(self):
self.quantity -= 1
Re: Please Help With My Python Shopping Cart Script by shady1234(op): 5:40pm On Mar 15, 2017
.
Re: Please Help With My Python Shopping Cart Script by shady1234(op): 5:40pm On Mar 15, 2017
shady1234:
So I have been able to solve this. For those who are still having problems with this test. The problem was in the remove_item method. If you use the supplied quantity to compute the total, you'll get a wrong answer if the quantity supplied is greater than what is already in the cart. The correct remove_item method is:


def remove_item(self, item_name, quantity, price):
if item_name and quantity and price:
if type(item_name) == str and type(quantity) == int and type(price) == int:
if item_name in self.items:
if quantity >= self.items[item_name]:
self.total -= (self.items[item_name] * price)
del self.items[item_name]
else:
self.items[item_name] -= quantity
self.total -= quantity * price

Re: Please Help With My Python Shopping Cart Script by noordean(m): 6:28pm On Mar 15, 2017
[quote author=shady1234 post=54620330][/quote]Your code has been cutt-off. Maybe you should attach a screenshot
1 Reply

My Python JourneyHow Can I Set Up Or Run My Python IDE On VS Code On Windows 8Please Review My Price comparison engine234

The Sad Truth About Programming Career In NigeriaI Need A Thrift App[News] Mark Zuckerberg’s Sister Is Now A Google Employee