I am new to programming and was creating a portion of a program for this project: http://www.reddit.com/r/beginnerprojects/comments/19ot36/project_a_variation_of_21/ when I ran into a index error. Using Stackoverflow answers, I originally corrected the error by making
random.randrange(0,len(cards)+1)]
instead of
random.randrange(0,53,1)]
However it continues to give me this error. If you run it 50 times it might not give you an error, but it might give you an error the first or fifth time you run it too. For the round function, I want to be able to deal cards from a single deck, each time I draw a card it removes it from the deck. Any advice would be most appreciated! - Thomas
Error Message:
Traceback (most recent call last):
File "<string>", line 420, in run_nodebug
File "<module2>", line 39, in <module>
File "<module2>", line 30, in round
File "<module2>", line 26, in draw
IndexError: list index out of range
Code:
def round():
cards = ["2", "2", "2", "2", "3", "3", "3", "3", "4", "4", "4", "4", "5",
"5", "5", "5", "6", "6", "6", "6", "7", "7","7","7","8", "8", "8", "8", "9",
"9", "9", "9", "10", "10", "10", "10", "Jack", "Jack", "Jack", "Jack",
"Queen", "Queen", "Queen", "Queen", "King", "King", "King", "King", "Ace", "Ace",
"Ace", "Ace"]
def draw():
return cards[random.randrange(0,len(cards)+1,1)]
acards = []
aroundscore = 0
acards.append(draw())
acards.append(draw())
print(acards)
print(acards)
cardsvalues = {"2":2, "3":3, "4":4, "5":5, "6":6, "7":7, "8":8, "9":9,
"10":10, "Jack":10, "Queen":10, "King":10, "Ace":1}
for i in acards:
print(cardsvalues[i])