Python 3: List index out of range -


every time run command, can't through of cards without having error; indexerror: list index out of range.

import random  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', 'j', 'j', 'j', 'j', 'q', 'q', 'q', 'q', 'k', 'k', 'k', 'k', 'a', 'a', 'a', 'a']  randomness = 51  while true:      cardindex = random.randint(0, randomness)     del cards[cardindex]     randomness = randomness -1     print(cards[cardindex]) 

print cards[cardindex] before deleting index:

while cards: # because need stop somewhere     cardindex = random.randint(0, randomness)     print(cards[cardindex])     del cards[cardindex]     randomness = randomness -1 

and don't need randomness @ all:

while cards:     cardindex = random.randrange(len(cards))     print(cards[cardindex])     del cards[cardindex] 

you'd using either random.sample:

for c in random.sample(cards, len(cards)):     print(c) 

or random.shuffle (which modify cards list):

random.shuffle(cards) c in cards:     print(c) 

Comments

Popular posts from this blog

python - pip install -U PySide error -

arrays - C++ error: a brace-enclosed initializer is not allowed here before ‘{’ token -

cytoscape.js - How to add nodes to Dagre layout with Cytoscape -