Day 044 - Project 44 : Bingo Game

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

hi guys! i’m struggling with day 44 exercise. the error i’m getting is “list index out of range” on line 39.
could someone help me out please?

print("--- David's Nan Bingo Card Generator ---")
print()
import random, os, time

bingo = []

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

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

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

  numbers.sort()

  bingo = [ [numbers[0], numbers[1], numbers[2]],
            [numbers [3], "BINGO", numbers [4]],
            [numbers[5], numbers[6], numbers[7]]
          ]
  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"

  exes = 0
  for row in bingo:
    for item in row:
      if item == "X":
        exes += 1

  if exes == 8:
    print ("you win")
    break
  
  time.sleep(1)
  os.system("clear")

This is an example of why formatting code correctly is so important, not only does it make it look nice and easier to read for others (and yourself), bad formatting, like in this case, can sometimes be the cause of bugs (especially since Python syntex uses whitespaces).

This works fine:

print("--- David's Nan Bingo Card Generator ---")
print()
import random, os, time

bingo = []


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


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


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

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


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"

	exes = 0
	for row in bingo:
		for item in row:
			if item == "X":
				exes += 1

	if exes == 8:
		print("you win")
		break

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

Hi there!

Could someone please explain why I cannot do this when looking for a number in my bingo card and replace it with “X”

  for row in bingolist:
    for item in row:
      if bingolist[row][item] == number:
        bingolist[row][item] = "X"
        Xs += 1

I’ve seen I’m supposed to use range(3), which makes sense but doesn’t look natural to me. It means you need to know the number of rows and items in your list when replacing an item?

Have a nice day!

A for in loop in Python iterates through items, not indexes, so bingolist[row][item] will likely throw an error since you’ll be trying to access something like bingolist[[ 83, 24, 37 ]][37]. Check this out on W3Schools (I recommend trying the Try it yourself code as you’ll get a better understanding of what I mean).

2 Likes

Many thanks @MattDESTROYER !

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