Quantcast

A community for students. Sign up today!

Here's the question you clicked on:

55 members online
  • 0 replying
  • 0 viewing

halkazi

  • one year 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
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 1

    You forgot to attach the program.

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

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

    Thanks!

  4. bdean20
    • one year 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
    • one year 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.

    Search OpenStudy
    • Attachments:

Ask your own question

Ask a Question
Find more explanations on OpenStudy

Your question is ready. Sign up for free to start getting answers.

spraguer (Moderator)
5 → View Detailed Profile

is replying to Can someone tell me what button the professor is hitting...

23

  • Teamwork 19 Teammate
  • Problem Solving 19 Hero
  • You have blocked this person.
  • ✔ You're a fan Checking fan status...

Thanks for being so helpful in mathematics. If you are getting quality help, make sure you spread the word about OpenStudy.

This is the testimonial you wrote.
You haven't written a testimonial for Owlfred.