Learn to Code via Tutorials on Repl.it!

← Back to all posts
How To Create a Code Cracker Game in Python

Code Cracker Tutorial
This tutorial is meant for beginners to intermediate. This tutorial is fairly simple, with some complex concepts to it. In all, this tutorial will get you on your way to learning Python. Without further ado, let's get on with it!

Go here for the output:

If you prefer a video version of this tutorial here it is 😀! Please like and subscribe!

The code on GitHub:

The code on Repl.it:

Objective: Generate a random password which is 3 to 5 integers long. Based on the length of the password, we generate the number of lives considering the fact that 'n' integer long password can have n! possible ways of arranging those 'n' numbers. The user has to guess the password based on the computer generated hints provided.

First, click on the plus sign in the bottom-right corner of your screen.

Then, click "Python3" in "Popular", or you can search.

Now let's get coding!
First, we need to import the modules needed for our code. So type in:
import random
import time
Now we can use the files that the creators made in our code! So we do not have to reinvent the wheel.

Let's make our functions to make our program more reusable and easy to understand. We will call these functions later when displaying our hints.

Now, we need to let the player have a certain number of tries before the game ends. This is why we need the factorial function. To make it, type in this code:

This code will basically puts this equation into code:


or in visuals:


Thanks to Wikipedia for the image.

What this code does is that when n, the parameter that the player will input, is 0, the program will stop. If that fails to be true, it subtracts itself until it becomes 0. This is what's called a recursive function (a function which either calls itself or is in a potential cycle of function calls).

Now we have to check if the number is even, so we create a function called even. To do so, we type in the following code:

Quite straightforward, when the value inputted divided by two leaves a remainder of 0, it returns True, telling the program that the number inputted is even.

Now, we need to make a function that counts the number of even numbers. To do so, we type in the following code:

What this code does is that we use the previous function for them to see if it is even, and it moves the variable isEven up by one. Finally it returns isEven.

We also need to make the function that finds the sum. To do so, we type the following code:

In this code, we make the value of the sum in a variable called sum because "sum" is a function in python. And we can't assign a variable the name of a function. This code returns the sum.

The final function we are going to make is the product of all the numbers. This works like Sum(), except with multiplication. This is the code:

Yay!!! 😀 You made all the functions! Now that we've made them, it's time to call them and display them on the screen. We will use concatenation to join the words together, or in other words, concatenate.

On this line, we choose the length of the code.
integer = random.randint(3, 5)
This creates a list of numbers to choose from.
chars= "1234567890"
This line creates the password.
password = random.sample(chars, integer)
This determines the lives the player will have.
lives = factorial(integer)
This defines the sum.
_sum_ = Sum(password)
time.sleep(random.randint(0, 5))
Taking out all the weird characters (Delete the next line and put print(password) instead and you'll see what I mean.)
password = ''.join(password)
Notifying player the code is generated.
Print the hints.

Checking if the player has any lives left. If not, it breaks the program, stopping the game.

AND THAT'S IT! You made it! 👍

Also, I put a video tutorial on youtube about this, on a channel called funcoding14. The link to it is above. However, I am moving it to a channel called Tech Tips, in where I will also make 3d modeling tutorials. I hope you enjoyed this tutorial. If you viewed this tutorial, please upvote it. It means a lot.


@laksh5 if you add in the code formatting i'll move this to Learn :)


@timmy_i_chen Ok I am adding the code formatting after almost 2 years XD. Can you put this in learn?


@laksh5 Hello???


@laksh5 I don't work at repl.it anymore, so I can't move your post - you'll have to ask one of the community moderators!


@laksh5 Who can I ask?


This is nice.

Also, you can format code inside your text so the identation doesn't get ruined. Here's an example: https://repl.it/talk/challenge/How-to-draw-Pixel-Art-on-Python-with-Turtle/7556.


@poacentro How can I do that?


@laksh5teach sorry, that is my other account.


@poacentro Thanks for linking me:)


@John_WardWard Thank you for upvoting!


This is a pretty fun game! Good luck with that tutorial!


@laksh5 Pls add markdown.


@John_WardWard yeah I did that