在python的甲板卡类

时间:2013-11-16 11:26:50

标签: python class random

我正在努力第一次创建一个类,我想我已经做了所有事情来让它运行,但我仍然遇到一堆问题'list' object has no attribute 'shffule'所以这里的问题是它不会洗牌,也不会告诉剩余的卡,任何人都可以告诉我,我做错了什么?提前致谢

 import random

 class card_deck:
     def __init__(self, rank, suite, card):


        self.rank= rank
         self.suite = suite
    def ranks(self):
         return self.rank
    def suites(self):
         return self.suite
     def cards(self,card):
         suit_name= ['The suit of Spades','The suit of Hearts', 'The suit of Diamonds','Clubs']
         rank_name=['Ace','2','3','4','5','6','7','8','9','10','Jack','Queen','King']


     def value(self):
         if self.rank == 'Ace':
             return 1
         elif self.rank == 'Jack':
             return 11
         elif self.rank == 'Queen':
             return 12
         elif self.rank == 'King':
             return 13
     def shffule(self):
         random.shuffle(self.cards)
     def remove(self,card):
         self.cards.remove(card)


     def cardremaining(self):
        self.suite-self.rank




 def main():
     try:
         deck=[]
         for i in ['Spades','Hearts', ' Diamonds','Clubs']:
         for c in ['Ace','2','3','4','5','6','7','8','9','10','Jack','Queen','King']:
                 deck.append((c, i))
         deck.shffule

         hand = []
         user =eval(input('Enter a number of cards: 1-7 '))
         print()
         while user <1 or user >7:
             print ("Only a number between 1-7:")
             return main()

         for i in range(user):
             hand.append(deck[i]) 
         print (hand)
     except ValueError:
         print("Only numbers")
         main()

3 个答案:

答案 0 :(得分:1)

除了包含许多小错误的代码之外;我会尽力回答你的主要问题。

如果您要使用shffule类的card_deck [sic] 方法,那么您首先需要创建该类的实例(而您尝试调用带列表的那个方法)。像这样:

deck = card_deck(some_rank,some_suit,some_card)
deck.shffule() #Now you can call the method

现在,既然你把它变成了一个类实例,就不能像hand.append(deck[i])一样从中获取项目 除非您在类定义中定义了方法__getitem__,否则:

#this will be in your class definition
def __getitem__(self,i):
    return self.card_list[i] #Of course you have to define a list of cards in your class too.

在我看来,你应该花更多的时间来了解如何定义类,方法如何工作以及如何访问类的成员。之后你会在这里做得更好

答案 1 :(得分:0)

您实际上从未创建过card_deck类的实例。表达式

deck=[] 

创建一个名为deck的变量,引用一个空列表。

在python中,[a,b,c,...]是创建列表文字的语法。

答案 2 :(得分:0)

from collections import namedtuple

Card = namedtuple('Card', 'sign, value')  #  no need to write class to represent card
SIGNS = ['Hearts', 'Diamonds', 'Spades', 'Clubs']


class Deck:
    def __init__(self):
        self.cards = [Card(sign, value) for sign in SIGNS for value in range(2,
                                                                             11) +
                      'J Q K A'.split()]

    def __repr__(self):
        return str([str(card) for card in self.cards])

    def __len__(self):
        return len(self.cards)

    def __getitem__(self, item):
        return self.cards[item]

    def __setitem__(self, key, value):
        self.cards[key] = value


deck = Deck()

print deck[11]  # indexing works, prints Card(sign='Hearts', value='K')

print len(deck)  # prints 52

print deck[13:16]  # slicing works

import random

random.shuffle(deck)  # shuffle works using no extra code