Day 043 - Project 43 : Bingo Card Generator

If you have any questions, comments or issues with this project please post them here!

Hi. David’s solution doesn’t assure non-repeated numbers in the bingo list.
Here is my solution to cover that:

import random
print('Bingo card generator')
print()

numbers = []

while len(numbers) < 8:
  random_number = random.randint(0,90)
  if random_number not in numbers:
    numbers.append(random_number)

numbers.sort() 

bingo = [ [numbers[0], numbers[1], numbers[2]],                     
          [numbers[3], "BINGO", numbers[4]], 
          [numbers[5], numbers[6], numbers[7]] ]

for i in bingo:
  for j in i:
    print(j, end=' | ')
  print()  

It will be great to hear your comments about my code or any other solutions.

2 Likes

@segima,
nice one! tho i kinda didn’t get how it works :sweat_smile:id like to learn im still new if you don’t mind
for i in bingo:
for j in i :
then I’m printing j it’ll avoid existing numbers ? how the j’s cant be duplicated like there’s no command that explains this
if j is not in I print j
urs just printing the whole j’s ?(I’m not really sure sorry)

my solution was this ,

import random, os, time
print("Welcome to \033[32mB\033[31mi\033[34mn\033[35mg\033[36mo\033[31m!\033[0m")
print()
print("3")
time.sleep(1)
print("2")
time.sleep(1)
print("1")
time.sleep(1)
os.system("clear")
bingo=[]
def num():
  number=random.randint(0,90)
  return number
def style_print():
  for row in bingo:
    print(row)
numbers= []
chosen_numbers=[ ]
for number in range(8):
  if number not in bingo:
    numbers.append(num())
numbers.sort()
bingo=[ [numbers[0],numbers[1],numbers[2]],
        [numbers[3], "BINGO!", numbers[4]],
        [numbers[5],numbers[6],numbers[7]] ]
print("\033[32mB\033[31mi\033[34mn\033[35mg\033[36mo\033[31m!\033[0m")
print()
style_print()
print("You WOn")

iv made another empty list to hold to already selected vars then used not in to append to the list then call those which are not appended to the already called list

1 Like

Hi, @Rocko. Thanks for your comment.

The while loop is what assures non-repeated numbers. It iterates until the list length is eight, then it appends that number to the list only if it isn’t already in there.

The for loop with the two variables (i, j) is only to print out the bingo card.

PS: It’s way better inserting your code here as preformatted text.

@segima ,
oh haha ya I completely avoided that len part LOL I only checked the i, j var as I did my repeat check on the var zone xD. my mistake I see you put len in here but up until the project 43 len was probably not introduced to us ik len now as iv used it to sum other projects I’m on day 66 now :grin:and Lol ik the reformat thingy I just copy pasted my code so didn’t bother but anyways thnx for your suggestion duly noted.

nice solution, i completely forgot about checking for duplicates. i did it with a counter.

import random

BingoList=[['','',''],['','',''],['','','']]
NumberList=[]

def PrettyPrint():
  print("-------------------------")
  for k in range(3):
    print(f"|",end="")
    for l in range(3):
      if k==1 and l==1:
        print(f"{'BINGO':^7}|",end="")
      else:
        print(f"{BingoList[k][l]:^7}|",end="")
    print("\n-------------------------")

counter=0
while counter <9: 
  Number=random.randint(0,91)
  if Number not in NumberList:
    NumberList.append(Number)
    counter+=1

NumberList.sort()

counter=0
for k in range(0,3):
  for l in range(0,3):
    BingoList[k][l]=NumberList[counter]
    counter+=1  

print("BINGO Card Generator\n")
PrettyPrint()

my previous version that didn’t check for duplicates is different from david’s since i didn’t make an extra subroutine and just put the call in directly:

for i in range(9):
  NumberList.append(random.randint(0,91))

another difference is that i wanted to avoid simply calling every single entry of the list (which happens what the solution does) because i want to use something that seems scalable (i know, a bit early to think about something like that :sweat_smile:). i was surprised that the ‘prettyprint’ in the solution doesn’t look pretty at all though :wink:

1 Like

Hi guys,
I already know my code doesn’t meet acceptance criteria 4, but I don’t really understand why it does not execute .sort() properly for criteria 3. Could someone explain it to me please?

import random

bingoList = [[None, None, None],[None, None, None],[None, None, None]]

for i in range (3):
  number = random.randint(0,90)
  bingoList[0][i] = number

for i in range (2):
  number = random.randint(0,90)
  bingoList[1][i] = number

for i in range (3):
  number = random.randint(0,90)
  bingoList[2][i] = number

bingoList.sort()

print(f"""|{bingoList[0][0]:<8} | {bingoList[0][1]:^8} | {bingoList[0][2]:>8} |
---------------------------------
|{bingoList[1][0]:<8} | {'BINGO':^8} | {bingoList[1][1]:>8} |
---------------------------------
|{bingoList[2][0]:<8} | {bingoList[2][1]:^8} | {bingoList[2][2]:>8} |""")
1 Like

ok, did some testing, and it seems .sort() does not work with 2d lists.
1d list needs to be done at first place, sorted, than it needs to be converted to a 2d list…

have fun guys!

1 Like

Im beginning to get stuck at some point in the latest projects(totally normal I guess) , checking here for some ideas . Thanks everyone.

here is my code on day 43

import random
my2Dlist = [ [ None , None , None],
             [ None , None , None],
             [ None , None , None] ]

def prettyPrint():
  print (f"""{my2Dlist[0][0]:<8}|{my2Dlist[0][1]:^8}|{my2Dlist[0][2]:>8}
---------------------------
{my2Dlist[1][0]:<8}|{my2Dlist[1][1]:^8}|{my2Dlist[1][2]:>8}
---------------------------
{my2Dlist[2][0]:<8}|{my2Dlist[2][1]:^8}|{my2Dlist[2][2]:>8}""")
  

while True:
  for i in range(0,3):
    for j in range (0,3):
      my2Dlist[i][j]=random.randint(1,90)  
    my2Dlist[i].sort()
  break
my2Dlist[1][1]="BINGO"

prettyPrint()


1 Like

I confused a little bit and that because this is the first time i hear bingo is it a real game?

Hey @WaayeelkaCraxma!

BINGO is a real game played by many people. It is usually played in large groups and not online.

2 Likes

How do i allocate a place for each random number inside that 2D list we are supposed to create? Do I use the append() function?

2 Likes

The append() function is used to add elements to an existing list, but it’s typically used in a loop or by directly appending values one by one. If you know the specific indexes where you want to add elements to an existing 2D list, you can use append() . But, you can use this code:

import random

# dimensions for the 2D list.
num_rows = 3
num_columns = 4

random_2d_list = [[random.randint(1, 100) for _ in range(num_columns)] for _ in range(num_rows)]

for row in random_2d_list:
    print(row)
2 Likes

I received an error for the prettyPrint() command but when I compare my code to the answer it seems to be the same. However, I did notice that the prettyPrint()command seems to be nested within the parameters assigned as bingo at the bottom.

The AI debugger is also suggesting a solution that I can’t seem to implement:

“The error in your project is that the closing parenthesis for the bingo list is missing in the prettyPrint function call.”

Is this a formatting issue or something else?

Repl link:

import random

bingo = []

def ran():
  number = random.randint(1,90)
  return number

def prettyPrint():
  for row in bingo:
    print(row)

numbers = []
for i in range(8):
  numbers.append(ran())

numbers.sort()

bingo = [  [numbers[0], numbers[1], numbers[2]],
          [numbers[3], "BINGO", numbers[4]],
          [numbers[5], numbers[6], numbers[7] 
          ]

prettyPrint()

The AI is almost right, you’re missing a ] after numbers[7].

2 Likes

That worked, thanks!

1 Like

It’s best to format code like this well so that you can see it more clearly.
Correct code:

bingo = [
  [numbers[0], numbers[1], numbers[2]],
  [numbers[3], "BINGO",    numbers[4]],
  [numbers[5], numbers[6], numbers[7]],
]
3 Likes