Want a solution to this - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Homework (https://python-forum.io/forum-9.html) +--- Thread: Want a solution to this (/thread-1930.html) Pages:
1
2
|
Want a solution to this - Shazily - Feb-05-2017 Hi, Please I am having problem to create this 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. In the Shop class, override the remove_item method, such that calling Shop's remove_item with no arguments decrements quantity by one. Anyone can help please? RE: Want a solution to this - Ofnuts - Feb-05-2017 What code have you got so far? RE: Want a solution to this - Shazily - Feb-05-2017 this is what i have tried class ShoppingCart: items = {} def __init__(self, total = 0): self.total = total def add_item(self, item_name, quantity, price): self.item_name = item_name self.quantity = quantity self.price = price if not item_name in self.items: self.items[item_name] = quantity total = self.price*self.quantity + self.total self.items[self.item_name] = self.quantity def remove_item(self, item_name, quantity, price): self.item_name = item_name self.quantity = quantity self.price = price total = self.total - price*quantity if item_name in self.items: del self.items[item_name] return self.items def checkout(self, cash_paid): self.cash_paid = cash_paid return self.total - cash_paid if cash_paid < self.total: return "Cash paid not enough" class Shop(ShoppingCart): def __init__(self): self.quantity = 100 def remove_item(self, quantity): self.quantity -= quantity return self.quantity RE: Want a solution to this - wavic - Feb-05-2017 When you copy a code from some IDE it's done with all formatting. To paste it here use CTRL+SHIFT+v RE: Want a solution to this - Shazily - Feb-05-2017 this is what i come up with class ShoppingCart: items = {} def __init__(self, total = 0): self.total = total def add_item(self, item_name, quantity, price): self.item_name = item_name self.quantity = quantity self.price = price if not item_name in self.items: self.items[item_name] = quantity total = self.price*self.quantity + self.total self.items[self.item_name] = self.quantity def remove_item(self, item_name, quantity, price): self.item_name = item_name self.quantity = quantity self.price = price total = self.total - price*quantity if item_name in self.items: del self.items[item_name] return self.items def checkout(self, cash_paid): self.cash_paid = cash_paid return self.total - cash_paid if cash_paid < self.total: return "Cash paid not enough" class Shop(ShoppingCart): def __init__(self): self.quantity = 100 def remove_item(self, quantity): self.quantity -= quantity return self.quantity RE: Want a solution to this - wavic - Feb-05-2017 (Feb-05-2017, 07:01 AM)wavic Wrote: When you copy a code from some IDE it's done with all formatting. To paste it here use CTRL+SHIFT+v Within python tags RE: Want a solution to this - buran - Feb-05-2017 Do you have question? To give something to think about below is extract from your code: class ShoppingCart: items = {} def __init__(self, total = 0): self.total = total def add_item(self, item_name, quantity, price): self.item_name = item_name self.quantity = quantity self.price = price if not item_name in self.items: self.items[item_name] = quantity total = self.price*self.quantity + self.total self.items[self.item_name] = self.quantity if __name__ =='__main__': john_cart = ShoppingCart() tom_cart = ShoppingCart() john_cart.add_item('milk', 2, 2.50) print "John's cart ->{}".format(john_cart.items) print "John's total ->{}".format(john_cart.total) print "Tom's cart ->{}".format(tom_cart.items) Do you see the problem? Also note that your class constructor should not take any arguments as per the assignment.
RE: Want a solution to this - Shazily - Feb-05-2017 I see. Can you assist please RE: Want a solution to this - nilamo - Feb-06-2017 What's wrong with what you already have? What does it not do that it should, or what is it doing that it shouldn't? RE: Want a solution to this - buran - Feb-06-2017 (Feb-05-2017, 10:35 AM)Shazily Wrote: Can you assist pleaseyes, but I will not do it for you. 1. regarding the items - you should understand the difference class attributes vs instance attributes. In your code items is class attribute and as such it is available to all instances of the class, incl. to change it. You should make it instance attribute. 2.regarding total - you should understand the difference between instance attributes and names local to the respective function/method (i.e. self.total vs. total ). Also do you think you need to have item_name, quantity and price as instance attributes (i.e. they will change with every item you add to the car)?3.Note that your assignment request that your class constructor need not to take any arguments. Yours may take total... |