0
이 인쇄물을 제대로 얻을 수 있도록 도와 줄 사람이 있습니까?중첩 된 목록 인쇄 - Python
class Deck(object):
def __init__(self):
self.cards = []
from random import shuffle
shuffle(self.cards)
#views all cards in the deck
def view_deck(self):
for x in self.cards:
print(x.name)
#takes in an (x) integer and views the top x cards of the deck
def view_number_of_cards(self, cards_to_view):
for x in self.cards[:cards_to_view]:
print(x.name)
class Player(object):
def __init__(self):
self.hand = []
self.row_1 = []
self.row_2 = []
self.row_3 = []
self.row_4 = []
self.row_5 = []
self.rows = []
self.rows.append(self.row_1)
self.rows.append(self.row_2)
self.rows.append(self.row_3)
self.rows.append(self.row_4)
self.rows.append(self.row_5)
self.graveyard = []
self.deck = Deck()
#draw a card from deck to hand
def draw_card(self):
c = self.deck.cards
cardDrawn = c.pop(0)
self.hand.append(cardDrawn)
#shuffle deck
def shuffle_deck(self):
from random import shuffle
shuffle(self.deck.cards)
def play_card(self, card, row):
self.rows[row-1].append(card)
self.graveyard.append(card)
self.hand.remove(card)
def update(self):
i = 1
for x in self.rows:
print "Lane "+str(i)+": "+str(x[0]),
i = i+1
나는이 시도 :
x = Player()
x.deck.cards = [1, 2, 3, 4]
x.draw_card()
x.play_card(x.hand[0], 1)
x.rows
[[1], [], [], [], []]
x.update()
이 내가 인쇄하려고하면 제대로 작동하는 것 같군 콘솔에서
Lane 1: 1
Traceback (most recent call last):
File "<pyshell#5>", line 1, in <module>
x.update()
File "C:/Users/Carl/Desktop/try.py", line 53, in update
print "Lane "+str(i)+": "+str(x[0]),
IndexError: list index out of range
을 발생 "1 레인 :"+ 행 [ 0] [0] 등등. 그러나 x-list 범위에 확실히 다른 목록이 있기 때문에 어떤 이유로 나는 계속이 IndexError를 얻습니다. 최악의 경우 목록이 미리 정의되어 있으므로 (row_2 = []) "레인 2 :"가 인쇄되어야하지만 그럴 수는 없습니다. 도와 주셔서 감사합니다!
감사합니다. 웬일인지 나는 단지 그것이 공백으로 인쇄 할 것이다라고 생각했다. "레인 2 :"와 같은 것을 어떻게 할 것인가? –