1

I'm trying to piece together a small assignment in Python OOP, but I'm not sure where I'm wrong.

I have two classes: Shoe and Store. In the Shoe class I just create the Shoe, and that Store class is where I'm doing all the methods.

I'm trying to create an "add shoe" method that will check if the shoe exists in a given list, if not, it will add it. I'm checking if the shoe exists by comparing the shoeID object.

Here is my code:

class Shoe(object):
    def __init__(self, shoeID, shoePrice, shoeSize, quantity):
        self.shoeID = shoeID
        self.shoePrice = shoePrice
        self.shoeSize = shoeSize
        self.quantity = quantity

    def __str__(self):
        return "Shoe ID:", self.shoeID, "Shoe Price:", str(self.shoePrice), "Shoe Size:", str(self.shoeSize), "Quantity:", str(self.quantity)

class Store(object):
    def __init__(self):
        self.shoeList = []

    def __str__(self):
        return "Shoe list: " + self.shoeList

    def add_shoe(self, newShoe):
        for i in self.shoeList:
            if i.shoeID == newShoe.shoeID:
                print("Shoe already exists, updating quantity")
                i.quantity += newShoe.quantity
            else:
                print("This is a new shoe, adding it to the list")
                self.shoeList.append(i)
            return

This is my tester:

import shoes

testStore = shoes.Store()
shoe1 = shoes.Shoe(123, 100, 40, 2)

print(testStore.add_shoe(shoe1))

My output is always None. I tried changing a bunch of stuff but I guess I'm just missing something stupid that I don't see. I'd love to get some help.

Thanks!

Daniel
  • 621
  • 5
  • 22
  • you have "return" in your add_shoe function. "return" by itself returns None. It will always hit this return since it is indented on same level as "if" and "else" – Christian Sloper Aug 22 '19 at 13:55
  • 1
    the problem isn't the return, the problem is `for i in self.shoeList`. Since it's initialised to an empty list, the body of your loop will never run. E.g. see what happens if you try `for i in []: print('hello world')` – Dan Aug 22 '19 at 13:57

1 Answers1

2

you code has a lot of issues. I fixed all

class Shoe(object):
    def __init__(self, shoeID, shoePrice, shoeSize, quantity):
        self.shoeID = shoeID
        self.shoePrice = shoePrice
        self.shoeSize = shoeSize
        self.quantity = quantity

    def __str__(self):
        return "Shoe ID: {} Shoe Price: {} Shoe Size: {} Quantity: {}".format(self.shoeID, str(self.shoePrice),str(self.shoeSize), str(self.quantity)) 


class Store(object):
    def __init__(self):
        self.shoeDict = {}

    def __str__(self):
        return "Shoe list: " + "\n".join([str(i) for i in self.shoeDict.values()])

    def add_shoe(self, newShoe):
        if newShoe.shoeID in self.shoeDict:
            print("Shoe already exists, updating quantity")
            self.shoeDict[newShoe.shoeID].quantity += newShoe.quantity
        else:
            print("This is a new shoe, adding it to the list")
            self.shoeDict[newShoe.shoeID] = newShoe
        return

testStore = Store()
shoe1 = Shoe(123, 100, 40, 2)

testStore.add_shoe(shoe1)
testStore.add_shoe(Shoe(123, 100, 40, 2))
print(testStore)

This is a new shoe, adding it to the list Shoe already exists, updating quantity Shoe list: Shoe ID: 123 Shoe Price: 100 Shoe Size: 40 Quantity: 4

galaxyan
  • 5,944
  • 2
  • 19
  • 43