두 개의 카드를 무작위로 처리하는 프로그램을 만들려고합니다. 그러나, 오류 팝업 계속 :유형 객체 x에는 속성이 없습니다. y
AttributeError: type object `CardPack` has no attribute `suits`
나는 잘 모르겠지만, 나는이 클래스 CardPack
이 값 suits
이없는 것을 의미합니다 생각합니다. 그러나 __init__
함수에서 정의됩니다. 나는 완전히 난감한 처지입니다.
클래스 코드 : 실행
import random
class CardPack:
def __init__(self):
self.cardsClubs = ["A C", "2 C", "3 C", "4 C", "5 C", "6 C", "7 C", "8 C", "9 C", "10 C", "J C", "Q C", "K C"]
self.cardsSpades = ["A S", "2 S", "3 S", "4 S", "5 S", "6 S", "7 S", "8 S", "9 S", "10 S", "J S", "Q S", "K S"]
self.cardsHearts = ["A H", "2 H", "3 H", "4 H", "5 H", "6 H", "7 H", "8 H", "9 H", "10 H", "J H", "Q H", "K H"]
self.cardsDiamonds = ["A D", "2 D", "3 D", "4 D", "5 D", "6 D", "7 D", "8 D", "9 D", "10 D", "J D", "Q D", "K D"]
self.cardsDealt = []
self.suits = ["C", "S", "H", "D"]
def getRandomCards(self):
card = ""
if random.choice(self.suits) == "C":
card = random.choice(self.cardsClubs)
elif random.choice(self.suits) == "S":
card = random.choice(self.cardsSpades)
elif random.choice(self.suits) == "H":
card = random.choice(self.cardsHearts)
elif random.choice(self.suits) == "D":
card = random.choice(self.cardsDiamonds)
else:
pass
return card
def deal(self):
cards = 1
while cards != 3:
self.cardsDealt[cards] = self.getRandomCards(self)
return self.cardsDealt
파일 생성 예 :
#DEALING TEST
from DeckDealer import CardPack
Dealer = CardPack
cards = Dealer.deal(CardPack)
print("Your cards are %s and %s!" % (cards[1], cards[2]))
난 당신이'의미 생각 판매점 = CardPack()' –
'dict' 사용을 고려 셀프 카드 = { 'C': [ 'A C', '2 C', ...], 'S': [...],}'그리고 나중에'suit = random.choice (('C', 'S', 'H', 'D')); 카드 = random.choice (self.cards [suit])'. 또한, 동일한 카드를 두 번이나 (컴퓨터를 상대로 연주하는 경우에는 더 이상) 다루지 않는 코드는 없습니다. – gboffi
또 다른 코드 검토 의견은 if-stack에서 무작위로 선택한 슈트를 반복하지 마십시오! 한 번 양복을 선택하고 if-stack에서 양복 값을 사용하고 싶습니다. – gboffi