Veneer Project Step 33

Link to the project here.

The code has been working until this step, but when using the buy_artwork method the artwork doesn’t change hands nor is remove from listings.

Here’s the current code. I really don’t know why is not working.

def buy_artwork(self, artwork):
      if artwork in veneer.listings:
        art_listing = None
        print(art_listing)
        for listing in veneer.listings:
          print(listing)
          if listing.art == artwork:
            art_listing = listing
            break
        if art_listing != None:
          art_listing.art.owner = self
          veneer.remove_listing(art_listing)


veneer = Marketplace()
edytta = Client("Edytta Halpirt", None, False)
moma = Client("The MOMA", "New York", True)
girl_with_mandolin = art("Picasso, Pablo", "Girl with a Mandolin (Fanny Tellier)", "oil on canvas", "1910", edytta)
edytta.sell_artwork(girl_with_mandolin, "$6M (USD)")
veneer.show_listings()
moma.buy_artwork(girl_with_mandolin)
print(girl_with_mandolin)
veneer.show_listings()

I think it may be useful to see the rest of the code. Of course, the error could very well be in this code.

There’s a couple of possibilities as to what could be happening (depending on how the rest of the code is written).

  • the remove_listing() function may not be working as intended
  • the setup for listing.art, listing.art.owner could have an error in the code.
  • your conditionals are failing, to which you can for example: print(listing.art == artwork) to verify art_listing = listing is happening, and the same for art_listing != None… if the first one doesn’t run, the second one won’t for sure (which sounds like the surface of what you described, at the very least).
1 Like

rest of the code:

class art:
  def __init__(self, artist, title, medium, year, owner):
    self.artist = artist
    self.title = title
    self.medium = medium
    self.year = year
    self.owner = owner
  def __repr__ (self):
    return "{artist}. \"{title}\". {year}, {medium}. {owner}, {location}.".format(artist=self.artist, title=self.title, year=self.year, medium=self.medium, owner=self.owner.name, location=self.owner.location)

class Marketplace:
  def __init__(self):
    self.listings = []
  def add_listing(self, new_listing):
    self.listings.append(new_listing)
  def remove_listing(self, listing):
    self.listings.remove(listing)
  def show_listings(self):
    for listing in self.listings:
      print(listing)

class Listings:
  def __init__(self, art, price, seller):
    self.art = art
    self.price = price
    self.seller = seller
  def __repr__(self):
    return "{art}. {price}".format(art = self.art.title, price = self.price)

class Client:
  def __init__(self, name, location, is_museum):
    self.name = name
    self.is_museum = is_museum
    if is_museum:
      self.location = location
    else:
      self.location = "Private Collection"
  def sell_artwork(self, artwork, price):
    if artwork.owner == self:
      new_listing = Listings(artwork, price, self)
      veneer.add_listing(new_listing)
  def buy_artwork(self, artwork):
    if artwork.owner != self:
      if artwork in veneer.listings:
        artwork.owner = self
        veneer.remove_listing(art_listing)


veneer = Marketplace()
edytta = Client("Edytta Halpirt", None, False)
moma = Client("The MOMA", "New York", True)
girl_with_mandolin = art("Picasso, Pablo", "Girl with a Mandolin (Fanny Tellier)", "oil on canvas", "1910", edytta)
edytta.sell_artwork(girl_with_mandolin, "$6M (USD)")
veneer.show_listings()
moma.buy_artwork(girl_with_mandolin)
print(girl_with_mandolin)
veneer.show_listings()

Thanks! I think it’s something on the conditionals, but I don’t understand what it is and the walkthrough doesn’t offer an explanation.

If you can’t spot it by eye then it’d be worth checking what those variables actually reference with print or something along those lines. If they’re not the problem then you at least you can start working backwards from there.

2 Likes

I did that, but still don’t understand it why the conditionals are not working

Try printing listing.art, try printing artwork. If they are textually the same, then try printing their types.

The conditionals don’t work, because at some point the data you expected to be there was misrepresented. On a personal note, if I decide to follow a tutorial I try to do so in spurts while I can verify that the code they’re putting in works. This helps prevent me from mistyping and then later have to dig the code for a missing . or } or “”. It happens to many of us at one point or another!

1 Like

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.