Here's the question you clicked on:

55 members online
  • 0 replying
  • 0 viewing

halkazi

  • 3 years ago

Python: Classes : Can someone tell me why my "Remove Queen of Clubs" is not working in the attached program?

  • This Question is Closed
  1. bdean20
    • 3 years ago
    Best Response
    You've already chosen the best response.
    Medals 1

    You forgot to attach the program.

  2. halkazi
    • 3 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

  3. halkazi
    • 3 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    Thanks!

  4. bdean20
    • 3 years ago
    Best Response
    You've already chosen the best response.
    Medals 1

    When you're storing each card in the deck, what you're storing are card objects, each is a specific instance. When you create a new one and don't add it to the deck, it's not finding the exact card. The problem is that it doesn't distinguish between whether a card is the same based on the value, it distinguishes them based on where they're stored in memory. Here's some console output to demonstrate the differences python sees: >>> Card("Q", "C") <__main__.Card object at 0x02BD1ED0> >>> Card("Q", "C") <__main__.Card object at 0x02BD1FF0> >>> Card("Q", "C") <__main__.Card object at 0x02BD1C10> >>> Card("Q", "C") <__main__.Card object at 0x02BD1FB0> >>> Card("Q", "C") <__main__.Card object at 0x02BD1ED0> >>> Card("Q", "C") <__main__.Card object at 0x02BD1FF0> Card("Q", "C") Each of these cards has identical value but the objects are not identical. To remove a card using the remove function, you need to remove the same card you inserted - the same object from the same address. You can check every card and remove the one with the correct rank/suit combo. Here's the code to do that: def removeCard(self, cardToRemove): for card in self.cards: if card.suit == cardToRemove.suit and card.rank == cardToRemove.rank: self.cards.remove(card)

  5. halkazi
    • 3 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    Thanks. That made complete sense, and worked like a charm.

  6. Not the answer you are looking for?
    Search for more explanations.

    • Attachments:

Ask your own question

Sign Up
Find more explanations on OpenStudy
Privacy Policy