How to Create a Simple Memory Game in Python

Python Memory Game Heading Image

Want to have a better memory? It’s been said that playing memory games helps. In this post, we build a simple memory game in Python. This game will test your memory by giving a series of strings for you to remember until you can’t do it anymore.

*Disclaimer: memory capacity improvement not guaranteed.

How to Create a Python Memory Game

We need three libraries to create a memory game in Python. We need the random library to generate random letters. The string library provides an easy way to pass a set of lowercase letters. The last library we need is the time library to enforce a time limit. These are all built-in Python libraries so you don’t have to install any external libraries.

We run the game in a play_game function that takes no parameters. We start by setting k, the number of letters displayed at once, to 3. Then we set loss, tracking whether the user has lost or not, to False and get the list of lowercase letters from the string library.

While the user hasn’t lost the game, we show a randomly generated string of length k for three seconds at a time. Then, we clear the screen by printing a ton of new lines. I’m using a terminal that’s small enough to be cleared with 100 lines. When the user inputs the correct string, we increment k by 1. As long as the user keeps winning, we keep playing. When the user misses a letter, we tell the user their score and end the game.

I run this site to help you and others like you find cool projects and practice software skills. If this is helpful for you and you enjoy your ad free site, please help fund this site by donating below! If you can’t donate right now, please think of us next time.

Yujian Tang

Leave a Reply

%d bloggers like this: