[Tutor] Help!! Code ridden with Bugs

Mats Wichmann mats at wichmann.us
Thu Mar 9 14:47:24 EST 2017

On 03/09/2017 12:32 PM, Joel Goldstick wrote:
> On Thu, Mar 9, 2017 at 8:28 AM, Eloka Chima via Tutor <tutor at python.org> wrote:
>> I am new to programming but an immersive  study in the past few weeks have brought me to speed so I can play around with codes but not really mastered them.My assignment below is ridden with bugs and I've done so much to get rid of them but to no avail.
>> Below is the project :
>> Create a class called ShoppingCart.
>> Create a constructor that takes no arguments and sets the total attribute to zero, and initializes an empty dict attribute named items.
>> Create a method add_item that requires item_name, quantity and price arguments. This method should add the cost of the added items to the current value of total. It should also add an entry to the items dict such that the key is the item_name and the value is the quantity of the item.
>> Create a method remove_item that requires similar arguments as add_item. It should remove items that have been added to the shopping cart and are not required. This method should deduct the cost of the removed items from the current total and also update the items dict accordingly.
>> If the quantity of an item to be removed exceeds the current quantity of that item in the cart, assume that all entries of that item are to be removed.
>> Create a method checkout that takes in cash_paid and returns the value of balance from the payment. If cash_paid is not enough to cover the total, return "Cash paid not enough".
>> Create a class called Shop that has a constructor which takes no arguments and initializes an attribute called quantity at 100.
>> Make sure Shop inherits from ShoppingCart.

that is an odd relationship (I realize it's part of the assignment as
you've posted it)... a "Shop" is conceptually a bigger thing than a
Shopping Cart what with overall inventories, item descriptions as well
as an overall financial picture; you'd almost think the shopping cart
would be the subclass.

>> In the Shop class, override the remove_item method, such that calling Shop's remove_item with no arguments decrements quantity by one'''

>>   def checkout(self, cash_paid):
>>     self.cash_paid = cash_paid
>>     if self.cash_paid < self.total:
>>       self.total -= self.cash_paid
>>       return self.total
>>       return "Cash paid is not enough"

Here you can't return two things, once you return, you are out of the
function, there's no chance to return a second time. Perhaps you meant
to have the second return be behind an "else:" statement? I think you
must not be expected to take this part of the description this
literally, because a financial total amount and a text string are
different types of objects, while Python's typing system makes it
possible to return either, that doesn't make it a good idea in your API
- how are you going to describe to users of this function that it might
be a totel, but then again it might be an error message? Giving some
thought to how to have a consistent return value and still meet the
requirements seems a good idea.

Have some other thoughts as well... in your Shop derived class (see
above), were you expecting the base class (ShoppingCart) initializer to
also run? If so, you better call it.

More information about the Tutor mailing list