Building Bingo with Python

Want to build your own bingo game? Python is a good way to get started. You can put together a basic version without getting bogged down in complicated code, then add features once the thing actually works.

There’s a bit more to it than picking random numbers. You need to generate cards, keep track of calls and check winning combinations. Get those bits right, and you’ve got the foundations of a proper game.

Get the numbers sorted

First, install Python and open an editor.

For a standard 75-ball bingo game, you need a pool of numbers from 1 to 75 and Python’s random.sample() can keep shuffling them in a sequence without repeats.

import random
numbers = random.sample(range(1, 76), 75)
def call_number():
    return numbers.pop()
print(call_number())

Each time you call the function, it removes and returns another number. It’s all simple enough, and you won’t call the same number twice. You might run out of numbers though, so just remember to check that the list isn’t empty before trying to draw again.

Making the bingo card

Next, you need a card. A standard 75-ball card has five columns labelled B, I, N, G and O, with five spaces in each column, each column has its own number range, and the centre square is usually free.

Python lists work well for storing the grid. You can use a nested list to represent the rows and columns, then update the card when a called number matches one of its entries.

This is the developmental start of an online bingo game and uses the same basic code can be expanded to generate multiple cards and handle several players.

Checking for a winner

Now it’s kind of important that your program needs to know when someone has bingo.

You can write a function that checks each row, column and diagonal against the marked squares. If every required square is marked, the function returns True, otherwise, the game carries on.

Keep the checking logic separate from the number caller. It makes testing easier, and you’ll have fewer headaches when you start adding different winning patterns.

Take it a step further

Once the basic version works, you can add a scoreboard, player names and a graphical interface. Python’s Tkinter library is one option for creating buttons and windows.

Using Flask you can create the web-based application. But running a multiplayer bingo service requires more than simply a website, as you now have to take care of the shared state of the game, calling synchronisation between players, etc. 

You have to be prepared to overcome common issues such as having no numbers left or receiving two calls at almost the same time. 

It is advisable to begin with just one card and one player to check how numbers draw and how game wins can be established. 

Python will do a lot of heavy work for you but the rules must be correct. Start with the basics and then you will be able to add more players or improve your application’s interface.

Pankaj Kumar
Pankaj Kumar

I have been working on Python programming for more than 12 years. At AskPython, I share my learning on Python with other fellow developers.

Articles: 259