Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8953027
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T14:00:24+00:00 2026-06-15T14:00:24+00:00

Can someone help me get this print properly? class Deck(object): def __init__(self): self.cards =

  • 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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T14:00:25+00:00Added an answer on June 15, 2026 at 2:00 pm

    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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Could someone please help explain why I can't get this to work? I properly
Im a beginner to css, please can someone help me get this element to
not able to get this, can someone help for this LINQ query? select col1,
I just cannot get this to work, would appreciate if someone can help. So
Hope someone can help i cant seem to get my head around this, i
Can someone help me understand how to write this case statement properly its not
Can someone help me get started with a button timeout feature. All I want
Please, can someone help me, how to get information of doc status... Generally, I
This maybe too localized, but I am hoping someone can help me articulate my
HI all Can someone please please help me been trying to suss this for

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.