list index out of range

Georgy Panterov gpanterov at yahoo.com
Tue May 13 14:39:27 EDT 2008


I am a relatively new python user. I am writing an economic simulation of a card-game. The simulation runs fine for a few iteration but then it gives an error of "list index out of range." 

The strange thing is that it will sometimes run for 10 iterations sometimes for only a few and sometimes won't run at all (seemingly arbitrary). 
Here is some of the code:

for _ in range(100):
    handA=deal_hand(DECK) #deals 2 'hole' cards from a DECK
    handB=deal_hand(DECK) 
    print handA
    print handB
    deal_flop(handA,handB,DECK) #appends 3 cards to handA and handB
    deal_current(handA,handB,DECK) #appends one more card
    deal_rake(handA,handB,DECK) #appends one more card
    DECK.extend(list(set(handA+handB))) #returns the cards to the DECK
    print winner(handA,handB) #returns 0-draw 1 -playerA win, 2-playerB win

The error message is :

['08C', '10H']
['07S', '03C']
--------
['06C', '02H']
['04D', '12S']
--------
['11H', '14S']
['06S', '04S']
--------

Traceback (most recent call last):
  File "G:\Other Stuff\POKER4.py", line 267, in <module>
    handB=deal_hand(DECK)
  File "G:\Other Stuff\POKER4.py", line 13, in deal_hand
    HAND.append(deck[i])
IndexError: list index out of range

In this case it ran 3 times. It will sometimes run for up to 9-10 times. It seems to be arbitrary but I can't get it to run more than 12 times.

Here is the code for the --deal_***-- functions:

def deal_hand(deck):
    HAND=[]
    for _ in range(2):  
        i=random.randint(0,len(deck)) #produces a random card from the deck
        HAND.append(deck[i]) # appends the card to the players' hand list
        deck.remove(deck[i]) #removes the card from the deck
    return HAND

def deal_flop(hand1,hand2,deck):
    for _ in range(3):
        i=random.randint(0,len(deck))
        
        hand1.append(deck[i])
        hand2.append(deck[i])
        deck.remove(deck[i])

I would appreciate any help with this
Thanks
George


Send instant messages to your online friends http://uk.messenger.yahoo.com 



More information about the Python-list mailing list