Cannot get created class method to work in Python

  • (2 Pages)
  • +
  • 1
  • 2

17 Replies - 980 Views - Last Post: 16 November 2012 - 12:57 PM Rate Topic: -----

#1 Stephenfrazier10  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 109
  • Joined: 12-September 12

Cannot get created class method to work in Python

Posted 14 November 2012 - 07:36 PM

So Im trying to create a playing card class in Python, and I have everything working fine except for one part I cannot figure out. I do not know what is wrong with my flip or setFaceUp method. When I save the class and run the test program, everything comes out perfectly, except where it is supposed to be flipped. It is supposed to have a name representing its value when its face up, and just "##" when its face down. Meaning that if I make a card object called S12 = Card("S13") when I do print(S12) it should print the name, (which it does). But then if I call on the flip method, it should simulate flipping the card over, so if I try to print it again, it just comes up as "##" (Which it also does). The problem is, once its flipped over, I cannot get it to flip back to its original name. If I say S12.flip again and then print(S12) again, it still says "##", but its supposed to go back to its old name of S12. Sorry for such a long post, but its hard to explain. Anyone have any idea?
Here is my class:
class Card:
    def __init__(self, name):
        self.name = name
       
        
    def __str__(self):
        description = self.name
        return description
    
    def getName(self):
        """
        Returns the name of the card object
        """
        return self.name
    
    def getSuit(self):
        """
        Returns the suit of the card object
        """
        a = self.name[0]
        return a

    def getFaceVal(self):
        """
        Returns the Face val of the card object
        """
        b = self.name[1:]
        return b

    def getFullFaceName(self):
        c = int(self.getFaceVal())
        if c == 11:
            fullName = "Jack"
        elif c == 12:
            fullName = "Queen"
        elif c == 13:
            fullName = "King"
        elif c == 1:
            fullName = "Ace"
        else:
            fullName = str(c)
        return fullName

    def getFullSuitName(self):
        ActualSuit = self.name[0]
        if ActualSuit in "Ss":
            FSN = "Spades"
        elif ActualSuit in "Dd":
            FSN = "Diamonds"
        elif ActualSuit in "Cc":
            FSN = "Clubs"
        elif ActualSuit in "Hh":
            FSN = "Hearts"
        return FSN

    def flip(self):
        e = self.isFaceUp()
        if e == True:
            self.name = "##"
        else:
            self.name = self.name
      
            
    def isFaceUp(self):
        if self.name == "##":
            return False
        else:
            return True

    def setFaceUp(self,truthValue):
        if truthValue == True:
            self.name = self.name
        else:
            self.name = "##"



Is This A Good Question/Topic? 0
  • +

Replies To: Cannot get created class method to work in Python

#2 Python_4_President  Icon User is offline

  • D.I.C Regular

Reputation: 53
  • View blog
  • Posts: 321
  • Joined: 13-August 11

Re: Cannot get created class method to work in Python

Posted 14 November 2012 - 07:55 PM

You did see that in the else part of flip you're saying,

self.name = self.name



right?

That means that if name == "##", isFaceUp returns false, and you hit the else part of flip, which effectively does nothing... Might be a good place to focus your attention.


also, check this out:
def setFaceUp(self,truthValue):
        if truthValue == True:
            self.name = self.name
        else:
            self.name = "##"



Isn't that the same as saying this?
def setFaceUp(self, truthValue):
    if not truthValue:
        self.name = "##"


This post has been edited by Python_4_President: 14 November 2012 - 07:58 PM

Was This Post Helpful? 1
  • +
  • -

#3 Stephenfrazier10  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 109
  • Joined: 12-September 12

Re: Cannot get created class method to work in Python

Posted 14 November 2012 - 07:59 PM

View PostPython_4_President, on 14 November 2012 - 07:55 PM, said:

You did see that in the else part of flip you're saying,

self.name = self.name



right?

That means that if name == "##", isFaceUp returns false, and you hit the else part of flip, which effectively does nothing... Might be a good place to focus your attention.


Yeah I know, How would I go about saying that if its face down, flip it back up? That is what Im having trouble with.
Was This Post Helpful? 0
  • +
  • -

#4 Stephenfrazier10  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 109
  • Joined: 12-September 12

Re: Cannot get created class method to work in Python

Posted 14 November 2012 - 08:12 PM

Is there a way to store a value? For example if before I started the method I said O = self.name, then somehow stored O, so that when self.name changed to "##" it would still have the value of the original name.
Was This Post Helpful? 0
  • +
  • -

#5 Python_4_President  Icon User is offline

  • D.I.C Regular

Reputation: 53
  • View blog
  • Posts: 321
  • Joined: 13-August 11

Re: Cannot get created class method to work in Python

Posted 14 November 2012 - 08:12 PM

What it looks like is: You've omitted a necessary piece of the puzzle. How can you get the original name back after you overwrite it with "##", since an ace could be "##", a king "##", a jack "##", a= 9 "##", and so on?

You can't getFaceVal because that would return "#".
You can't getFullFaceName (sensibly, anyway) because "#" is 35, which doesn't match any of your tests except the last one, which would just return "35".
You can't even get the suit, because "#" applies to all things which are *hint* not currently being displayed.


If I'm correct, you're more concerned with flipping the card any way you can, than with what you're going to end up needing to do after you figure that part out.

I might suggest you, instead, try to figure out what you would need to do in an ideal world, where you CAN flip a card.


Let me ask you this... Can you get the name, suit, or face value of a card that is face down?

This post has been edited by Python_4_President: 14 November 2012 - 08:17 PM

Was This Post Helpful? 1
  • +
  • -

#6 Stephenfrazier10  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 109
  • Joined: 12-September 12

Re: Cannot get created class method to work in Python

Posted 14 November 2012 - 08:19 PM

No you cannot, because self.name would be "##". However with this simple project it is supposed to just return # or ## for all tests when its face down. I just have to be able to flip it back over, which I can't figure out how to do.
Was This Post Helpful? 0
  • +
  • -

#7 Python_4_President  Icon User is offline

  • D.I.C Regular

Reputation: 53
  • View blog
  • Posts: 321
  • Joined: 13-August 11

Re: Cannot get created class method to work in Python

Posted 14 November 2012 - 08:37 PM

class SecretMessage(object):
    def __init__(self):
        self.message = "You're almost there."
        self.statuses = {"folded":False, "unfolded":True}
        self.status = self.statuses['folded']

    def foldMessage(self):
        if self.status:
            self.status = self.statuses['folded']

    def unfoldMessage(self):
        if not self.status:
            self.status = self.statuses['unfolded']

    def readMessage(self):
        if self.status:
            print self.message
        else:
            print "Cannot read the message! It is folded!"
        


Was This Post Helpful? 1
  • +
  • -

#8 Stephenfrazier10  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 109
  • Joined: 12-September 12

Re: Cannot get created class method to work in Python

Posted 14 November 2012 - 08:40 PM

That is what I was going to originally try, however we have to find a way to do it in one method not two. If I could of done two different methods it would of been a lot easier, but we have to do one method called flip that can flip it back and forth.
Was This Post Helpful? 0
  • +
  • -

#9 Python_4_President  Icon User is offline

  • D.I.C Regular

Reputation: 53
  • View blog
  • Posts: 321
  • Joined: 13-August 11

Re: Cannot get created class method to work in Python

Posted 14 November 2012 - 08:54 PM

I know =)
That's why I did it the way I did.

Bang your head against that wall until you get it. It's there, I promise.

Let me ask you this,

What is the opposite of True?
What is __str__?

This post has been edited by Python_4_President: 14 November 2012 - 08:56 PM

Was This Post Helpful? 1
  • +
  • -

#10 Stephenfrazier10  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 109
  • Joined: 12-September 12

Re: Cannot get created class method to work in Python

Posted 14 November 2012 - 09:00 PM

IS there something really obvious I do not know? I have been at this for about 3 hours lol, I cannot figure it out...

lol..... hmmm I think False is the opposite of True and __str__ is what you get when you print the object.
Was This Post Helpful? 0
  • +
  • -

#11 Python_4_President  Icon User is offline

  • D.I.C Regular

Reputation: 53
  • View blog
  • Posts: 321
  • Joined: 13-August 11

Re: Cannot get created class method to work in Python

Posted 14 November 2012 - 09:12 PM

If a equals b, then what is a if we say a now equals the opposite of b?

What is a? (not b, and not not b, but a itself)


(Oh yeah, it's super obvious, BTW. But there are three things you need to get a handle on before you can see it.)

This post has been edited by Python_4_President: 14 November 2012 - 09:12 PM

Was This Post Helpful? 1
  • +
  • -

#12 Stephenfrazier10  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 109
  • Joined: 12-September 12

Re: Cannot get created class method to work in Python

Posted 14 November 2012 - 09:15 PM

I am so lost.... Is there something in Python that Im not using that can do it for me that Im just not using, or is it just something Im not doing right?
Was This Post Helpful? 0
  • +
  • -

#13 Stephenfrazier10  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 109
  • Joined: 12-September 12

Re: Cannot get created class method to work in Python

Posted 14 November 2012 - 09:26 PM

Ok, so my brain feels like its bleeding on the inside lol, but I got something going now. I changed somethings around, is this what you were trying to get at?
class Card:
    def __init__(self, name):
        self.name = name
        
       
        
    def __str__(self):
        if self.isFaceUp == True:
            description = self.name
        else:
            description = "##"
        return description
    
    def getName(self):
        """
        Returns the name of the card object
        """
        return self.name
    
    def getSuit(self):
        """
        Returns the suit of the card object
        """
        a = self.name[0]
        return a

    def getFaceVal(self):
        """
        Returns the Face val of the card object
        """
        b = self.name[1:]
        return b

    def getFullFaceName(self):
        c = int(self.getFaceVal())
        if c == 11:
            fullName = "Jack"
        elif c == 12:
            fullName = "Queen"
        elif c == 13:
            fullName = "King"
        elif c == 1:
            fullName = "Ace"
        else:
            fullName = str(c)
        return fullName

    def getFullSuitName(self):
        ActualSuit = self.name[0]
        if ActualSuit in "Ss":
            FSN = "Spades"
        elif ActualSuit in "Dd":
            FSN = "Diamonds"
        elif ActualSuit in "Cc":
            FSN = "Clubs"
        elif ActualSuit in "Hh":
            FSN = "Hearts"
        return FSN

    def flip(self):
        if self.isFaceUp == True:
            self.name = "##"
        else:
            self.setFaceUp(True)
      
            
    def isFaceUp(self):
        if self.name == "##":
            return False
        else:
            return True

    def setFaceUp(self,truthValue):
        if truthValue == True:
            self.isFaceUp = True
        else:
            self.isFaceUp = False


Was This Post Helpful? 0
  • +
  • -

#14 Python_4_President  Icon User is offline

  • D.I.C Regular

Reputation: 53
  • View blog
  • Posts: 321
  • Joined: 13-August 11

Re: Cannot get created class method to work in Python

Posted 14 November 2012 - 09:32 PM

>>> a = True
>>> a = not a
>>> a
False



Boolean conditionals, part of Operators, expressions, and program flow.

You're getting really close now, but you're still overwriting the name, which is, to be blunt, pretty damn critical to the usefulness of your class. You really should not do that.

Here's a tip:
you can say,
if True == True:
    print "Yeah, we got that..."

#and you can say

if True:
    print "Huh, how bout that?"




Also:
	        if self.isFaceUp == True:
	            description = self.name
	        else:
	            description = "##"
	        return description




This is really unnecessary.

All you're trying to do is return a string based on whether or not the card is face up or down.

if self.isFaceUp:
    return the string for card being up
#implicit else, because returning from a function prevents execution of ANYTHING after that statement
return the string for card NOT being up.


This post has been edited by Python_4_President: 14 November 2012 - 09:46 PM

Was This Post Helpful? 1
  • +
  • -

#15 Stephenfrazier10  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 109
  • Joined: 12-September 12

Re: Cannot get created class method to work in Python

Posted 14 November 2012 - 09:44 PM

I GOT IT! lol finally! It really was so simple, now I feel like an idiot lol and BTW I would love to build the robot army lol
class Card:
    def __init__(self, name):
        self.name = name
        
       
        
    def __str__(self):
        if self.isFaceUp == True:
            description = self.name
        else:
            description = "##"
        return description
    
    def getName(self):
        """
        Returns the name of the card object
        """
        return self.name
    
    def getSuit(self):
        """
        Returns the suit of the card object
        """
        a = self.name[0]
        return a

    def getFaceVal(self):
        """
        Returns the Face val of the card object
        """
        b = self.name[1:]
        return b

    def getFullFaceName(self):
        c = int(self.getFaceVal())
        if c == 11:
            fullName = "Jack"
        elif c == 12:
            fullName = "Queen"
        elif c == 13:
            fullName = "King"
        elif c == 1:
            fullName = "Ace"
        else:
            fullName = str(c)
        return fullName

    def getFullSuitName(self):
        ActualSuit = self.name[0]
        if ActualSuit in "Ss":
            FSN = "Spades"
        elif ActualSuit in "Dd":
            FSN = "Diamonds"
        elif ActualSuit in "Cc":
            FSN = "Clubs"
        elif ActualSuit in "Hh":
            FSN = "Hearts"
        return FSN

    def flip(self):
        if self.isFaceUp == True:
            self.setFaceUp(False)
        else:
            self.setFaceUp(True)
      
            
    def isFaceUp(self):
        if self.name == "##":
            return False
        else:
            return True

    def setFaceUp(self,truthValue):
        if truthValue == True:
            self.isFaceUp = True
        else:
            self.isFaceUp = False



Thanks for the Help!
Was This Post Helpful? 1
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2