0

Can someone help me get this print properly?

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

When I try this:

x = Player()
x.deck.cards = [1, 2, 3, 4]
x.draw_card()
x.play_card(x.hand[0], 1)
x.rows
[[1], [], [], [], []]
x.update()

This happens

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

In the console it seems to be working properly if I try to print "Lane 1: "+rows[0][0] etc but for some reason I keep getting this IndexError which doesn't make sense to me because there are definitely other lists in the x-list range. at worst, because the lists are predefined (row_2 = []) then it should print "Lane 2: " but that doesn't even happen. Thanks for the help!

1 Answer 1

2

The problem is that, as you say, row_2 = []. Since it's empty, it has no element at index 0.

To get blank "Lane x:" lines you could rewrite update like so:

def update(self):
    for x in self.rows:
        for i in range(5):
            print("Lane {}: ".format(i), end='')
            if len(x):
                print(x[0])
            else:
                print()

You'll also need to add an import at the beginning of the file to get the print function instead of the print statement:

from __future__ import print_function
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks that makes sense. For some reason I thought it would just print blank. something like "Lane 2: " any idea on how to do that?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.