我正在第一次创建一个类,我在想我已经做了所有事情来使它运行,但是我仍然遇到很多问题,这些问题是'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()
最佳答案
除了您的代码中包含许多小错误外;我将尝试回答您的主要问题。
如果要使用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.
我认为,您应该花更多的时间来了解如何定义类,方法如何工作以及如何访问类的成员。之后,您将在这里做得更好