[Tutor] Help!! Code ridden with Bugs

Alan Gauld alan.gauld at yahoo.co.uk
Thu Mar 9 15:43:12 EST 2017

On 09/03/17 13:28, Eloka Chima via Tutor wrote:
> My assignment below is ridden with bugs

So tell us what they are don;t make us guess and
don't expect us to run code which is by your
own admission faulty! If you get error messages
post them, in full.

If it runs but misbehaves tell us what happens
 - formats your hard drive? Destroys your
graphics card? prints a wrong value? What?

> class ShoppingCart(object):
>   def __init__(self):
>     self.items = {}
>     self.total = 0
>   def add_item(self,item_name,quantity,price):
>     self.item_name = item_name
>     self.quantity = quantity
>     self.price = price 

I don;t think you need these as instance variables,
you can just use the values in the parameter.

>       if not self.item_name in self.items:
>         self.items[self.item_name] = self.quantity
>         self.total +=self.price

So this becomes...

       if not item_name in self.items:
         self.items[item_name] = quantity
         self.total += price

>   def remove_item(self,item_name,quantity,price):
>     self.item_name = item_name
>     self.quantity = quantity
>     self.price = price

Same thing here

>       for self.item_name in self.items:

You probably shouldn't use an instance variable in
a for loop, that's likely to cause some confusing
conditions as it will remember the loop value
after you exit the method.

>         if self.item_name in self.items:

This will always be true because you are setting
the name in the loop above, so you don't need
to check it.

>           del self.items[self.item_name]

And for the same reason this will delete all
the items because they are all in the list.

>           self.total -= self.price
>           if self.quantity > self.items:
>             self.items = 0

I'm not sure what you think this doing...

>     if self.cash_paid < self.total:
>       self.total -= self.cash_paid
>       return self.total
>       return "Cash paid is not enough"

You can't have two returns like this. Well,
technically you can, but the second one will
never be reached.

That's as far as I got. Please re-post with
some more specific information about what
you think is wrong.

Alan G
Author of the Learn to Program web site
Follow my photo-blog on Flickr at:

More information about the Tutor mailing list