Hello everyone,

I did have a look around with search option to see if I could get an answer their, but I couldn't quite find my issue(at least I don't think I did). This is for a final project for school where I have to create a class for a deck of cards and let the user do several things with it. One of these things is cutting the deck so that the top 26 cards(assuming this is a standard 52 card dec)are now on the bottom. This is where the problem comes up.

def cut_deck(self):
        cut_deck = []
        index = 0
        for loop in range(26, 52):
            cut_deck[index] == self.__cards[loop]
            index = index + 1
        for loop in range(0, 26):
            cut_deck[index] == self.__cards[loop]
            index = index + 1
        self.__cards = cut_deck

The self.__cards array is initialized earlier on in the class and it sets up fine. This is the only part that does not work for me. I keep getting an error 'IndexError: list index out of range'. I took a look on google and I did find some similar issues, but I still can't figure out the problem. Any help would be greatly appreciated. Thank you in advance.

Assuming self.cards is a list:

cards = range(1, 53)
print cards

cards_shuffled = cards[26:] + cards[:26]
print cards_shuffled
#
# or to modify your code  (this assumes cards are numbered 1-52, not 0-51)
        cut_deck = []
        for loop in range(26, 53):   ## 53, __not__ 52 = 1-52
            cut_deck.append(self.__cards[loop])
        for loop in range(1, 27): 
            cut_deck.append(self.__cards[loop])

Edited 5 Years Ago by woooee: n/a

This article has been dead for over six months. Start a new discussion instead.