list index out of range

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Georgy Panterov

    list index out of range

    I am a relatively new python user. I am writing an economic simulation of acard-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 sometimesfor 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(ha ndA,handB,DECK) #appends one more card
    deal_rake(handA ,handB,DECK) #appends one more card
    DECK.extend(lis t(set(handA+han dB))) #returns the cards to the DECK
    print winner(handA,ha ndB) #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(dec k[i])
    IndexError: list index out of range

    In this case it ran 3 times. It will sometimes run for up to 9-10 times. Itseems 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.randin t(0,len(deck)) #produces a random card from the deck
    HAND.append(dec k[i]) # appends the card to the players' hand list
    deck.remove(dec k[i]) #removes the card from the deck
    return HAND

    def deal_flop(hand1 ,hand2,deck):
    for _ in range(3):
    i=random.randin t(0,len(deck))

    hand1.append(de ck[i])
    hand2.append(de ck[i])
    deck.remove(dec k[i])

    I would appreciate any help with this
    Thanks
    George


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

    #2
    Re: list index out of range

    random.randint( x,y) returns a random integer between _and including_ x and
    y, so randint(0, len(deck)) might return len(deck) and since python's
    indices start at 0, the maximum index is len(deck)-1.

    rather than using randint(0,len(d eck)-1) you could just do
    card = random.choice(d eck)
    HAND.append(car d)
    deck.remove(car d)

    although it would be more efficient to do remove card by index which then
    gets us back to using randint, like this

    i = random.randint( 0, len(deck)-1)
    HAND.append(dec k[i])
    del deck[i]

    but i guess speed probably isn't a big factor here

    also you could have done
    i=random.randin t(0, len(deck)-1)
    HAND.append(dec k.pop(i))






    "Georgy Panterov" <gpanterov@yaho o.comwrote in message
    news:mailman.10 68.1210704380.1 2834.python-list@python.org ...
    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(ha ndA,handB,DECK) #appends one more card
    deal_rake(handA ,handB,DECK) #appends one more card
    DECK.extend(lis t(set(handA+han dB))) #returns the cards to the DECK
    print winner(handA,ha ndB) #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(dec k[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.randin t(0,len(deck)) #produces a random card from the deck
    HAND.append(dec k[i]) # appends the card to the players' hand list
    deck.remove(dec k[i]) #removes the card from the deck
    return HAND

    def deal_flop(hand1 ,hand2,deck):
    for _ in range(3):
    i=random.randin t(0,len(deck))

    hand1.append(de ck[i])
    hand2.append(de ck[i])
    deck.remove(dec k[i])

    I would appreciate any help with this
    Thanks
    George


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


    Comment

    • Mike Kent

      #3
      Re: list index out of range

      On May 13, 2:39 pm, Georgy Panterov <gpante...@yaho o.comwrote:
      >
      def deal_hand(deck) :
      HAND=[]
      for _ in range(2):
      i=random.randin t(0,len(deck)) #produces a random card from the deck
      ^ Here i can be from 0 thru (the number of cards in the
      deck).
      HAND.append(dec k[i]) # appends the card to the players' hand list
      ^ Here you index into the deck to get a
      card. The legal indexes are 0 thru (number of cards in the deck - 1)
      What happens if i is (the number of cards
      in the deck)?
      deck.remove(dec k[i]) #removes the card from the deck
      return HAND
      >

      Comment

      Working...