【问题标题】:If-statement does not work(?)如果语句不起作用(?)
【发布时间】:2013-09-04 19:56:15
【问题描述】:

我是一个快乐的业余爱好者,他试图创造一个“或多或少”的游戏。我在积分分配上是不对的。我的 if 语句无法正常工作。我没有收到错误消息,但一切都在“其他”中运行。条件永远不会满足,虽然K、Q、J、Ace是随机的……为什么?

class Card(object):

    totPoints = 0

    VALUE = {"A":1, "2":2, "3":3, "4":4, "5":5, "6":6, "7":7, "8":8, "9":9, "10":10, "J":10, "Q":10, "K":10}

    RANKS = ["A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"]

    SUIT = ["Python/projekt/bilder/hearts.png", "Python/projekt/bilder/spades.png", "Python/projekt/bilder/diamond.png", "Python/projekt/bilder/clubs.png"]

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

    def __str__(self):
        rep = self.rank + self.suit
        return rep

    def draw(self):

        bg = ImageTk.PhotoImage(Image.open(self.suit).resize((10, 10)))

        cardGraph = Canvas(win, width=70, height=100, bg="White", bd=1, relief='solid', highlightthickness=2)
        cardGraph.photo=bg
        cardGraph.pack(side = "left", anchor=NW)

class Hand(object):
    def __init__(self):
        self.cards = []

    def __str__ (self):
        if self.cards:
            rep = ""
            for card in self.cards:
                rep += str(card) + " "
        else:
            rep = "<empty>"
        return rep

    def clear(self):
        self.cards = []

    def add(self, card):
        self.cards.append(card)

    def give(self, card, other_hand):
        self.cards.remove(card)
        other_hand.add(card)

class Deck(Hand):

    def populate(self):
        for suit in Card.SUIT:
            for rank in Card.RANKS:
                self.add(Card(rank, suit))

    def shuffle(self):
        import random
        random.shuffle(self.cards)
        DrawCard = self.cards[0]
        DrawCard.draw()

    def deal(self, hands, per_hand = 0):
        for rounds in range(per_hand):
            for hand in hands:
                if self.cards:
                         top_card = self.cards[0]
                         self.give(top_card, hand)         
                else:
                    print("Cant continue deck. Out of cards!!")

    def setValue(self):
            if self.cards[0] == "K":
                Card.totPoints += 10
                print Card.totPoints
            elif self.cards[0] == "Q":
                Card.totPoints += 10
            elif self.cards[0] == "J":
                Card.totPoints += 10
            elif self.cards[0] == "A":
                Card.totPoints += 10
            else:
                Card.totPoints += self.cards
                print Card.totPoints

【问题讨论】:

  • 题外话:你的 if-elif 语句可以简化为:if self.cards[0] in ['k', 'Q', 'J', 'A']: Card.totPoints += 10
  • 你说的“虽然K、Q、J、Ace是随机的”是什么意思?你是说你的代码随机打印出来吗?打印代码的哪一部分?您能否将您的代码简化为无需图形即可运行的代码,以便我们对其进行测试?
  • 另外题外话:Hand.__str__ 可以写成if self.cards: return ' '.join(str(card) for card in self.cards) else: return '&lt;empty&gt;' 更好
  • 也许您的self.cardsCard 对象的列表,并且您正在将它们与字符串进行比较。然后比较失败。您必须将 if 条件更改为 self.cards[0].rank

标签: python class oop python-2.7 if-statement


【解决方案1】:

尝试重构它,让你在找到你想要的价值后返回;这使得它不太容易混淆 elif 的顺序。此外,对大量值进行“测试”更容易:

def setValue(self):
   first_card = self.cards[0]
   face_cards = ['K','Q','J','A']
   if first_card in face_cards: 
      card.totPoints += 10
      return
   card.totPoints += self.cards

顺便说一句:如果第一张牌是人脸牌,这将导致得分为 10,如果不是,则为所有牌的总分。那是你要的吗?

最后,您似乎正在从卡片组对象中更新卡片的值。或许您应该在 init 中设置卡片的分数,这样您就不必从外部进行操作?

【讨论】:

    【解决方案2】:

    您的代码绝不会将卡片仅表示为字符串。相反,您使用的是 Card() 类的实例:

    def populate(self):
        for suit in Card.SUIT:
            for rank in Card.RANKS:
                self.add(Card(rank, suit))
    

    您需要针对 .rank 属性进行测试

    def setValue(self):
        if self.cards[0].rank == "K":
            Card.totPoints += 10
            print Card.totPoints
        elif self.cards[0].rank == "Q":
            Card.totPoints += 10
        elif self.cards[0].rank == "J":
            Card.totPoints.rank += 10
        elif self.cards[0].rank == "A":
            Card.totPoints += 10
        else:
            Card.totPoints += int(self.cards[0].rank)
    

    注意.rank总是是一个字符串,所以当它是一个数字卡时,你需要把它变成一个整数。无论如何,我认为这是else: 分支的目标。

    您在该函数中的代码可以大大简化:

    def setValue(self):
        rank = self.cards[0].rank
        Card.totPoints += 10 if rank in 'KQJA' else int(rank)
    

    或者,您可以只使用已有的 Class.VALUE 映射:

    def setValue(self):
        Card.totPoints += Card.VALUE[self.cards[0].rank]
    

    【讨论】:

    • 非常感谢 =) 完美 =)
    猜你喜欢
    • 2012-12-26
    • 2013-06-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多