vendredi 30 octobre 2015

Random choice function

I am making a game using pygame based on the game war. I am getting an error when running the code for splitting my deck in the main loop (it is in its own file). The error says:

"/usr/local/Cellar/python/2.7.10_2/Frameworks/Python.framework/Versions/2.7/lib/python2.7/random.py", line 275, in choice
return seq[int(self.random() * len(seq))]  # raises IndexError if seq is empty
IndexError: list index out of range

The deck splitting file looks like this:

import Deck
from random import *
deck = Deck.deck
playerDeck = []
AIDeck = []

def splitDeck():
player_deck_count = 26

while player_deck_count > 0:

    transfer_card = (choice(deck))

    playerDeck.append(transfer_card)
    deck.remove(transfer_card)

    player_deck_count -= 1

AIDeck = deck
shuffle(playerDeck)
shuffle(AIDeck)


print "Player deck length:" + str(len(playerDeck))
print "AI deck length:" + str(len(AIDeck))
print "Deck length:" + str(len(deck))

The Deck file looks like this:

deck = [
    '2_c',
    '2_d',
    '2_h',
    '2_s',

I get that is has to do with the sequence (list is empty) but there is obviously still 26 cards in the original deck. I have tried changing when the while player_deck_count loop stops but with no luck. Thanks for the help in advance

P.S. just comment if you want the main loop.




Aucun commentaire:

Enregistrer un commentaire