A Bingo card consists of 5 columns of 5 numbers which are labelled with the letters
B, I, N, G and O.
In particular,
the numbers that can appear under the B, range from 1 to 15,
the numbers that can
appear under the I, range from 16 to 30,
the numbers that can appear under the N, range from 31 to 45, and so on.
Write a function that creates a random Bingo card and stores it in a dictionary. The
keys will be the letters B, I, N, G and O. The values will be the lists of five numbers.
Write a second function that displays the Bingo card
with the columns labelled appropriately.
Both both functions in the main function.
SOLUTION :
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from random import randrange | |
NUMS_PER_LETTER = 15 # range of numbers | |
# First Function Creation of a Bingo Card | |
def createCard(): | |
card = {} # dictionary to store characters as a KEY and list of numbers as a VALUE | |
# lower and upper bounds to generate random numbers | |
lower = 1 | |
upper = 1 + NUMS_PER_LETTER # last number not included that why added 1 | |
# characters stored in a list | |
bingo = ["B", "I", "N", "G", "O"] | |
# iterating list to adding characters with restpect to list of numbers | |
for char in bingo: | |
# added character | |
card[char] = [] | |
# generating 5 unique numbers | |
while len(card[char]) != 5: # will stop at length 5 | |
num = randrange(lower, upper) | |
# need to check for duplicacy | |
if num not in card[char]: | |
card[char].append(num) | |
# end while | |
# now need to change by lower and upper limits by added 15 | |
lower = lower + NUMS_PER_LETTER | |
upper = upper + NUMS_PER_LETTER | |
# end for | |
return card | |
# Second Function to Display the Bingo Card | |
def displayCard(card): | |
print("B I N G O") | |
for i in range(0, 5): # for moving into rows | |
for char in ["B", "I", "N", "G", "O"]: # getting characters | |
''' using placeholder %2d will create space for 2 digit | |
If single digit is there then space will be there | |
Using row index to get value from dictionary list | |
card[char][i] will give index value from each letter | |
''' | |
print("%2d " % card[char][i], end=" ") | |
print() | |
def main(): | |
card = createCard() # return dictionary card | |
displayCard(card) # passing to display function | |
# calling main | |
main() | |
OUTPUT :
Bingo Card
Reviewed by Leaf Code
on
August 07, 2020
Rating:

No comments: