How To Create a Code Cracker Game in Python
laksh5 (85)

Note: There may be some errors with indents in this tutorial. Correct accordingly.
Another Note: Go here for the output:
http://bit.ly/OutputCodeCracker

Yet Another Note: if you prefer a video version of this tutorial here it is 😀! Please like and subscribe!
http://bit.ly/CodeCrackerVideoTutorial

The code on GitHub:
http://bit.ly/GitHubCodeCracker

The code on Repl.it:
http://bit.ly/TheCodeOnReplit

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
else:
return n * factorial(n-1)

This code will basically puts this equation into code:
n!=n×(n−1)!
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
else:
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)
Loading
print('loading...')
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("CODE GENARATED")
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)
break
if answer != password:
lives -= 1
print('you have', lives, 'lives')
print('BOO')
if lives <= 1:
print ('GAME OVER!!!!!')
break

AND THAT'S IT! You made it! 😅😄😃😅😂😎👍

If you viewed this tutorial, please upvote it. It means a lot.

Also, I would like to give thanks to a special coding academy called "The Coder School". If you're a kid younger than 18 or 19 you should enroll yourself in there. I couldn't have done it without them.

You are viewing a single comment. View All