本文介绍了Python奇怪的结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 所以我有这个代码So I have this codeclass Deck(): deck=[] for i in range(1,15): deck.append(Card("red","heart",i)) deck.append(Card("red","diamond",i)) deck.append(Card("black","club",i)) deck.append(Card("black","spade",i)) #make our deck random random.shuffle(deck) #removing cards with number 11 because 1 and 11 cards are the same for i in deck: if i.number==11: deck.remove(i)#hand class creates hands of 5 cards, taking items from the list created in deck classclass Hand(): def get_hand(): #check if there are enough cards to make a new hand if len(Deck.deck)<5 : print("No more hands to deal") else: #get the first 5 items from our deck iterator = islice(Deck.deck, 5) #show the hand created for i in iterator: print('{} {} {}'.format(i.number,i.type,i.colour)) #delete the shown card from the deck Deck.deck.remove(i) 我开始发垃圾邮件后After I start to spamHand.get_hand() 直到我的52张牌中少于10张牌,它不再打印另外5张牌,而是4张牌一。这有什么不对? 我尝试过的事情: 我试图调整until there are less than 10 cards in my 52 cards deck, It won't print another 5 cards hand, but a 4 cards one. What is wrong here?What I have tried:I tried to tweak theif len(Deck.deck)<5 :行,但没有结果推荐答案import randomfrom itertools import isliceclass Card(): def __init__(self, color, suit, rank): self.color = color self.suit = suit self.rank = rank def display(self): print( '{2}-{0}-{1}'.format(self.color, self.suit,self.rank))class Deck(): def __init__(self): self.deck=[] for i in range(1,15): if i != 11: self.deck.append(Card("red","heart",i)) self.deck.append(Card("red","diamond",i)) self.deck.append(Card("black","club",i)) self.deck.append(Card("black","spade",i)) #make our deck random random.shuffle(self.deck)#hand class creates hands of 5 cards, taking items from the list created in deck classclass Hand(): def get_hand( theDeck ): #check if there are enough cards to make a new hand l = len(theDeck.deck) if l < 5: print("No more hands to deal") else: #get the first 5 items from our deck iterator = islice(theDeck.deck, 5) remain = slice(5,l) theDeck.deck = theDeck.deck[remain] #show the hand created for i in iterator: i.display()theDeck = Deck()for n in range (1,15): print("--------------------------------------") Hand.get_hand(theDeck) 这篇关于Python奇怪的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
06-27 12:12