473,320 Members | 1,820 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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(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. 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.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
Jun 27 '08 #1
2 3687
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(deck)-1) you could just do
card = random.choice(deck)
HAND.append(card)
deck.remove(card)

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(deck[i])
del deck[i]

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

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


"Georgy Panterov" <gp*******@yahoo.comwrote in message
news:ma***************************************@pyt hon.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(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
Jun 27 '08 #2
On May 13, 2:39 pm, Georgy Panterov <gpante...@yahoo.comwrote:
>
def deal_hand(deck):
HAND=[]
for _ in range(2):
i=random.randint(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(deck[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(deck[i]) #removes the card from the deck
return HAND
Jun 27 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Jess Austin | last post by:
hi, I like the way that Python does lists, and I love the way it does iterators. But I've decided I don't like what it does with iterators of lists. Lists are supposed to be mutable sequences,...
2
by: Steven Bethard | last post by:
Nick Coghlan wrote: > Hmm, it might be nice if there was a UserList.ListMixin that was the > counterpart to UserDict.DictMixin I've thought this occasionally too. One of the tricky issues...
23
by: comp.lang.tcl | last post by:
I have a TCL proc that needs to convert what might be a list into a string to read consider this: ]; # OUTPUTS Hello World which is fine for PHP ]; # OUTPUT {{-Hello}} World, which PHP...
0
by: cindy | last post by:
I have a dynamic datagrid. I have custom classes for the controls public class CreateEditItemTemplateDDL : ITemplate { DataTable dtBind; string strddlName; string strSelectedID; string...
3
by: Ant | last post by:
Hi, I'm using an array list as the collection for my indexer (_alPeople). When i try to set _alpeople to a value at a certain index, I get an error. I can't simply add the value as it needs to be...
3
bartonc
by: bartonc | last post by:
Whenever a function retrieves a list element outside of loop, you should wrap the reference in a try block instead of testing the length of the list (because you will probably forget to subtract one...
6
by: zfareed | last post by:
<code> #include <iostream> #include <fstream> const int MAX_LENGTH = 10; using namespace std;
7
by: Rehceb Rotkiv | last post by:
I want to check whether, for example, the element myList exists. So far I did it like this: index = -3 if len(myList) >= abs(index): print myList Another idea I had was to (ab-?)use the...
33
by: John Salerno | last post by:
Is it possible to write a list comprehension for this so as to produce a list of two-item tuples? base_scores = range(8, 19) score_costs = print zip(base_scores, score_costs) I can't think...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.