# Understanding code

##

# Create and display a random Bingo card.

#

from random import randrange

NUMS_PER_LETTER = 15

## Create a Bingo card with randomly generated numbers

# @return a dictionary representing the card where the keys are the strings ‘‘B’’, ‘‘I’’, ‘‘N’’,

# ‘‘G’’, and ‘‘O’’, and the values are lists of the numbers that appear under each letter

# from top to bottom

def createCard():

card = {}

# The range of integers that can be generated for the current letter

lower = 1

upper = 1 + NUMS_PER_LETTER

# For each of the five letters

for letter in ["B", "I", "N", "G", "O"]:

card[letter] = []

# Keep generating random numbers until we have 5 unique ones

while len(card[letter]) != 5:

next_num = randrange(lower, upper)

# Ensure that we do not include any duplicate numbers

if next_num not in card[letter]:

card[letter].append(next_num)

# Update the range of values that will be generated for the next letter

lower = lower + NUMS_PER_LETTER

upper = upper + NUMS_PER_LETTER

# Return the card

return card

## Display a Bingo card with nice formatting

# @param card the Bingo card to display

# @return (None)

def displayCard(card):

print("B I N G O")

# Display the numbers on the card

for i in range(5):

for letter in ["B", "I", "N", "G", "O"]:

print("%2d " % card[letter][i], end="")

print()

# Create a random Bingo card and display it

def main():

card = createCard()

displayCard(card)

# Call the main function only if this file has not been imported into another program

if __name__ == "__main__":

main()

I am looking at this code and trying to understand what this part does?

I have tried to check through the docs and videos and don’t see this used

https://docs.python.org/3.9/tutorial/datastructures.html

You’ve got two data structures, a dictionary which members have lists as values.

card[letter]       =>  refer to member in dictionary

card[letter][i]    =>  refer to element `i` in member list
1 Like

by member you mean item in a list right?

The above is a solution to an exercise but the code throws an error at this part and 2 more, so if the syntax below is correct, l don’t know why it does that

The errors are:

B I N G O
Traceback (most recent call last):
File “c:\Users\simmy.vscode\test3.py”, line 48, in
main()
File “c:\Users\simmy.vscode\test3.py”, line 45, in main
displayCard(card)
File “c:\Users\simmy.vscode\test3.py”, line 40, in displayCard
print("%2d " % card[letter][i], end="")
IndexError: list index out of range
PS C:\Users\simmy.vscode>

Thanks

Close, but no. It refers to a member of a dictionary (an attribute).

dic = {
'key': 'value'    <= member is a key/value pair
}

As for the errors, what is the error message?

1 Like

@mtf is absolutely right! As for the error, it appears you’re trying to use a list index which is bigger than the length of the list minus one:

some_list = [1, 2, 3, 4]#the length of this list is 4
#so the largest index we can use is 4-1 = 3, which means this throws an error:
some_list[4]

That would throw a very similar error to the one you’re getting:

IndexError: list index out of range