我是相对较新的Python,我正在尝试使用类和方法来找到排名和适合的卡

时间:2014-01-20 16:58:46

标签: python-2.7

我需要使用 str 方法查找卡的等级和套装我不确定我是否正确执行此操作。我在main中得到了正确的答案,但它没有从方法 str 中打印出我想要的语句。我错过了什么?

from math import *
from random import *
from string import * 

class Card:

    def __init__(self, rank, suit) :
        self.rank = int(rank)
        self.suit = str(suit) 

    def getRank(self) :
        return self.rank

    def getSuit(self):  
        return self.suit

    def __str__(self): 

        if self.rank == 1:
            if self.suit == "d" :
                print "The card is an Ace of Diamonds "
            elif self.suit == "c":
                print "The card is an Ace of Clubs"
            elif self.suit == "h" :
                print "The card is an Ace of Hearts"
            elif self.suit == "s" :
                print "The card is an Ace of Spades"

        elif self.rank in range(2, 11):
            if self.suit == "d" :
                print "The card is a", rank,  "of Diamonds"
            elif self.suit == "c" :
                print "The card is a", rank,  "of Clubs"            
            elif self.suit == "h" :
                print "The card is a", rank,  "of Hearts"
            elif self.suit == "s" :
                print "The card is a", rank,  "of Spades"

        elif self.rank == 11 :
            if self.suit == "d" :
                print "The card is a Jack of Diamonds"
            elif self.suit == "c" :
                print "The card is a Jack of Clubs"            
            elif self.suit == "h" :
                print "The card is a Jack of Hearts"
            elif self.suit == "s" :
                print "The card is a Jack of Spades"

        elif self.rank == 12 :
            if self.suit == "d" :
                print "The card is a Queen of Diamonds"
            elif self.suit == "c" :
                print "The card is a Queen of Clubs"            
            elif self.suit == "h" :
                print "The card is a Queen of Hearts"
            elif self.suit == "s" :
                print "The card is a Queen of Spades"

        elif self.rank == 13 :
            if self.suit == "d" :
                print "The card is a King of Diamonds"
            elif self.suit == "c" :
                print "The card is a King of Clubs"            
            elif self.suit == "h" :
                print "The card is a King of Hearts"
            elif self.suit == "s" :
                print "The card is a King of Spades"

        return self.rank, self.suit 

def main():

    #  You can also pick the cards at random using randrange() function 

    x = raw_input("Enter the rank and suit for the card that you picked (tab separated) ")
    print x 

    rank, suit = x.split("\t")
    print rank, suit 

    s = Card(int(rank), str(suit))

    print s.getRank(), s.getSuit(), s.__str__()

    if __name__ == '__main__' :
        main()

感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

我觉得你在代码块之外包含代码,这让我有点混淆我拼凑。

就你的代码而言,print语句对我有用。如果希望方法str中的print语句起作用,则需要调用它。

目前这是你的功能回归给我的:

3 h
3 h The card is a 3 of Hearts 
(3, 'h')

第一个“3小时”来自你的

print rank, suit
输入之后的

3小时这张牌是3张Heart来自来电

s.getRank() ## Returns 3
s.getSuit() ## Returns h
s.__str__()  ##Returns:
#The card is a 3 of Hearts 
#(3, 'h')

最后一个s。 str ()返回多个部分,因为你的return语句与print语句一起使用。我不确定您希望代码返回的格式,但我认为您只需要s。 str ()返回“The card is a 3 of Hearts”

目前它返回

self.rank, self.suit 

但那些已经被那个班级归还,所以我不确定他们为什么会在那里。你可以让它只返回你的print语句。

class Card:

    def __init__(self, rank, suit) :
        self.rank = int(rank)
        self.suit = str(suit) 

    def getRank(self) :
        return self.rank

    def getSuit(self):  
        return self.suit

    def __str__(self): 

        if self.rank == 1:
            if self.suit == "d" :
                printstatement= "The card is an Ace of Diamonds "
            elif self.suit == "c":
                printstatement=  "The card is an Ace of Clubs"
            elif self.suit == "h" :
                printstatement=  "The card is an Ace of Hearts"
            elif self.suit == "s" :
                printstatement=  "The card is an Ace of Spades"

        elif self.rank in range(2, 11):
            if self.suit == "d" :
                printstatement=  "The card is a "+ rank +" of Diamonds"
            elif self.suit == "c" :
                printstatement=  "The card is a "+ rank +" of Clubs"            
            elif self.suit == "h" :
                printstatement=  "The card is a "+ rank +" of Hearts"
            elif self.suit == "s" :
                printstatement=  "The card is a "+ rank +" of Spades"

        elif self.rank == 11 :
            if self.suit == "d" :
                printstatement=  "The card is a Jack of Diamonds"
            elif self.suit == "c" :
                printstatement=  "The card is a Jack of Clubs"            
            elif self.suit == "h" :
                printstatement=  "The card is a Jack of Hearts"
            elif self.suit == "s" :
                printstatement=  "The card is a Jack of Spades"

        elif self.rank == 12 :
            if self.suit == "d" :
                printstatement=  "The card is a Queen of Diamonds"
            elif self.suit == "c" :
                printstatement=  "The card is a Queen of Clubs"            
            elif self.suit == "h" :
                printstatement=  "The card is a Queen of Hearts"
            elif self.suit == "s" :
                printstatement=  "The card is a Queen of Spades"

        elif self.rank == 13 :
            if self.suit == "d" :
                printstatement=  "The card is a King of Diamonds"
            elif self.suit == "c" :
                printstatement=  "The card is a King of Clubs"            
            elif self.suit == "h" :
                printstatement=  "The card is a King of Hearts"
            elif self.suit == "s" :
                printstatement=  "The card is a King of Spades"

        return str(printstatement) 

如果您更改班级以反映上面发布的代码,则代码会输出:

7 h
7 h The card is a 7 of Hearts

如果您希望最终字符串位于其自己的行上,则可以执行以下操作:

## Change this:
print s.getRank(), s.getSuit(), s.__str__()  
## To this:
print s.getRank(), s.getSuit(), "\n", s.__str__()
## To have your code output this:
4 d
4 d 
The card is a 4 of Diamonds