Learn to Code via Tutorials on Repl.it!

← Back to all posts
How To Create a Code Cracker Game in Python
Logimite (188)

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:

def factorial(n):
    if n == 0:
        return True
        return n * factorial(n-1)

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:

def even(num):
        if num % 2 == 0:
                return True
                return False

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:

def countEven(password):
        isEven = 0
        for x in password:
                if even(int(x)):
                        isEven += 1
        return isEven

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:

def Sum(password):
        _sum_ = 0
        for x in password:
                _sum_ += int(x)
        return _sum_

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:

def product(password):
        producty = 1
        for x in password:
                producty *= int(x)
        return producty  

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.

print('it is', integer ,'numbers long')
print('the sum is', _sum_)
producty = product(password)
print('the product is', producty)
isEven = countEven(password)
print('The number of even numbers is', isEven)
print('The first number is', int(password[0]))
print('The last number is', int(password[-1]))
isOdd = integer - isEven
print('The number of odd numbers is', isOdd)
print('The highest number is', str(max(password)))
print('The lowest number is', str(min(password)))
print('you have', lives, 'lives') 

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

while lives > 0:
        answer = input('what is your password')
        if answer == password:
                        print('OMG, you got it ')
                        print('The password was', password)
        if answer != password:
                lives -= 1
                print('you have', lives, 'lives')
                if lives <= 1:
                        print ('GAME OVER!!!!!')

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.

manuelegentilon (3)

I would like to make the game infinite without closing the program.but how?

Logimite (188)

@manuelegentilon Add
while True:
to the beginning of your code after the imports.
Then, change

if answer == password:
        print("YAY! You're the best! YOU GOT IT!")


if answer == password:
        print("YAY! You're the best! YOU GOT IT!")

Easy fix. Also, thank you so much for viewing this. It has been so long since somebody saw this. Please upvote if this helped.

manuelegentilon (3)


hello i apply your modify but i see this error when i run the module

"expected an indented block"

Logimite (188)

@manuelegentilon After while True:, you need to indent everything after it. You can do that by doing Shift+Tab. Let me know if that helped. If it didn't, copy-paste from this repl

timmy_i_chen (1180)

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

Logimite (188)

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

timmy_i_chen (1180)

@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!

poacentro (0)

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.

minermaniac447 (283)

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