Day 044 - Project 44 : Bingo Game

Many thanks @MattDESTROYER !

Edit: this website is very helpful! Thanks for sharing!

Hi everyone! I’ve never seen the “global” in “global bingo” and David didn’t really touch on it in the video explanation. Can someone explain what it does? I couldn’t figure this challenge at all. I started with the previous day’s code plus the hangman code thinking I could use a “allBingo = True” to do the X replacing but it didn’t work out. Anyways, thank for the help!

Hi @laurenjane thanks for your message and apologies for the delay in responding to you. Can you please share a link to your Repl so I can explain using your code?

regarding the solution. i didn’t understand what the “while num in numbers:” in the createCard subroutine did, so i thought about it.

def createCard():
  global bingo
  numbers = []
  for i in range(8):
    num = ran()
    while num in numbers:
      num = ran()
    numbers.append(ran())

it took me a while to understand that it is supposed to prevent repetition of numbers, but doesn’t work. (tested via reduction of the range of the random numbers generated). it took another while for me to realize why:

numbers.append(ran())

should have been

numbers.append(num)

I was struggling and looked at the solution and I saw the global function which has not been used before. Can I know what it does?

Basically it changes the variable globally. Here’s a longer explanation:

1 Like

Hi
I have basically managed to make the whole game, however my counter system and breaking out of the loop to finish the game won’t work for some reason. I cross-referenced with the provided solution but for some reason I can’t get it to work.

import random, time, os
bingoCard = []

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

numbers.sort()

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

def printCard():
  for row in bingoCard:
    for number in row:
      print(number, end=' | ')
    print()

while True:
  print ("David's Nan's Bingo Card Generator")
  print()
  printCard()
  print()
  num = int(input("input number: "))
  for row in range(3):
    for item in range(3):
      if bingoCard[row][item] == num:
        bingoCard[row][item] = "X"

  counter = 0
  for row in bingoCard:
    for item in row:
      if item == "x":
        counter+=1
        
  if counter == 8:
    print("You won")
    break

  time.sleep(0.1)
  os.system("clear")

I think it might be that in your counter section, you have item == “x”, but earlier you have bingoCard[row][item]= “X”. the computer will read “x” and “X” as different things.

Oh, that was a stupid mistake lol
Thanks

Please help! I don’t understand why my card is printing out multiple times once a number is entered.
https://replit.com/@KristineCody/Nans-Bingo-Game-Day-44#main.py Any input is welcome! TIA

import random, time, os
row1=[]
row2=[]
row3=[]
numbers=[]
card=[]
def prettyPrint():
  print()
  for row in card:
    for item in row:
      print(f"{item:^10}", end =" | ")
    print()
  print()
      
def createCard():
  for i in range(3):
    row1.append(numbers[i])
  for i in range(3,6):
    row2.append(numbers[i])
  for i in range (6,9):
    row3.append(numbers[i])
  row2[1] = "BINGO"
  card.append(row1)
  card.append(row2)
  card.append(row3)
  
x=0  
print("\033[34mDavid's Nan's BINGO Game\033[0m")
for i in range (9):
  num = random.randint(1, 100)
  if num not in numbers:
    numbers.append(num)
numbers.sort()

#createCard()
#prettyPrint()
  
while True:
  if x!=8:
    createCard()
    prettyPrint()
    caller = int(input("Next Number: "))
    for i in range(len(numbers)):
      if caller == numbers[i]:
        numbers[i]="X"
        x+=1
        
  else:
    print("\033[35mBINGO!")

I suspect your error is that you’re re-creating the card, try this instead:

createCard()
#prettyPrint()
  
while True:
  if x!=8:
    #createCard()
    prettyPrint()
2 Likes

Question:
How to create 8 random numbers without any doubles and put them into a list. The code in lession day 44 breaks as soon as randint will generate 2 of the same numbers.

Repl link/Link to where the bug appears:
https://replit.com/@cbrenner/Day44100Days

Screenshots, links, or other helpful context:
If randint makes 2 same numbers, it will only give me 7 numbers in total. When checking the variables in the bingo card I’ll get an “out of range” after, as it expects to have 8 nr. in it.

numbers = []
for i in range(8):
  value = random.randint(1,90)
  while value not in numbers:
    numbers.append(value)    
numbers.sort()

You need to generate a new random number if it is a duplicate.

numbers = []
for i in range(8):
  value = random.randint(1,90)
  while value in numbers:
    value = random.randint(1,90)
  numbers.append(value)
numbers.sort()
2 Likes

If you’re aiming to practice algorithmic thinking (which can come in handy), it’s useful to understand how the above answer works, but for practical use, you just need random.sample, and sort (which you’ve already used)

1 Like

I see. Thanks. Was really confused by this one, now when I see the solution it’s logical.

Good to know too, but random.sample was not in the course so far.

1 Like

Repl link:

Hi everyone,
I have a query about the solution for the Bingo card Challenge (Day 44).
Below, a range function is used to substitute a number in the Bingo card with an “X” when that number is ‘called’. (continue):

createCard()
while True:
  prettyPrint()
  num = int(input("Next Number: "))
  for row in range(3):
    for item in range(3):
      if bingo[row][item] == num:
        bingo[row][item] = "X"

I struggle to understand why the below doesn’t work: (continue)

num = int(input("Next Number: "))
  for row in bingo:
    for item in row:
      if item == num:
        item = "X"

despite the same ‘syntax’ is used in Line 10-13 and 41-43:

for row in bingo:
    for item in row:
      print(item, end="\t|\t")

for row in bingo:
    for item in row:
      if item=="X":

Thank you in advance for your help

Hi @marcoichnusa2 , welcome to the forums!
Do you get an error message?

It doesn’t crush, but it doesn’t turn the called number on the Bingo card into an “X” either

item is a temporary variable. Instead you should be setting bingo[row][item] like this code:

2 Likes