Home > OS >  What does this index error mean, " list index out of range on line 17"
What does this index error mean, " list index out of range on line 17"

Time:10-12

So I am building a program where it has to print out each card from each suit in a deck of cards (11 represents a jack, 12 a queen, etc.). And the output has to show from 2 to 14 (which I don't know how to do). Here is what I have so far:

import random

DECK = 52 

suits = ['hearts', 'diamonds', 'clubs', 'spades'] 

card = ' '

suit_num = 0 

for i in range(DECK):    

    for cards in range(int(DECK/len(suits))): 

        print(str(cards)   ' '   suits[suit_num]) #line 17

        suit_num  = 1  

card = str((random.randrange(14)))   ' of '   str(suits[random.randrange(4)]) 

print('Your chosen card is the '   str(cards)) 

CodePudding user response:

@ScottC identified the issue in his comment. One way to avoid these types of problems is to avoid using indices, and Python makes avoiding them very easy.

Here's a quick re-write taking out the indexing for suits.

import random

DECK = 52 
suits = ['hearts', 'diamonds', 'clubs', 'spades'] 

for suit in suits:
    for each_card in range(DECK//len(suits)):
        print(str(each_card)   ' '   suit) 
        
card = f'{random.randrange(14)} of {random.choice(suits)}'
print('Your chosen card is the '   card)

EDIT -- To return the desired output of 2 to 14, then one solution would be as follows, but it would mean that a deck of more than 52 cards (if that were ever needed) would need addressing. Another question might be around jokers too.

import random

DECK = 52 
suits = ['hearts', 'diamonds', 'clubs', 'spades'] 

for suit in suits:
    for each_card in range(2, 15):
        print(str(each_card)   ' '   suit) 
        
card = f'{random.randrange(2, 15)} of {random.choice(suits)}'
print('Your chosen card is the '   card)

CodePudding user response:

There is only one list in your code, suits = ['hearts', 'diamonds', 'clubs', 'spades'], and this list has 4 elements. So, len(suits) is 4.

Look at the nested for loop:

for cards in range(int(DECK/len(suits))): 

        print(str(cards)   ' '   suits[suit_num]) 

        suit_num  = 1 

In theory, this loop runs 13 times, from 0 to 12. In each iteration, suit_num gets incremented by 1. This is a problem because suit_sum is actually the index of the list suits in the line above suit_num = 1 ! Since suits has size 4, its index cannot exceed 3. So, when the for loop runs for the 4th time, suit_num becomes 4, and you encounter a run-time error, "list-index out of range", because the list suits does not have an index greater than 3!

CodePudding user response:

To avoid invalid indices we can use modulus or remainder (%).

import random

DECK = 52

suits = ['hearts', 'diamonds', 'clubs', 'spades']

card = ' '

suit_num = 0

for i in range(DECK):
    for cards in range(DECK//len(suits)):
        print(str(cards)   ' '   suits[suit_num % len(suits)]) #line 17
        suit_num  = 1
print("="*40)
card = str((random.randrange(start=2, stop=15)))   ' of '   str(suits[random.randrange(start=2, stop=15) % len(suits)])

print('Your chosen card is the '   str(card))

(I just updated the code to match your 2-to-14 requirement.)

  • Related