Skip to content
Snippets Groups Projects
Commit e4825c67 authored by Joe Stephenson's avatar Joe Stephenson
Browse files

Added comments and TODOs to decoder puzzle

parent e6d688e2
Branches
No related tags found
No related merge requests found
......@@ -2,9 +2,6 @@ import random
password = "undecoded"
# these 2 functions are far too bulky, break them down into smaller functions
# could nest functions maybe? Not sure if that is good practice or not
def function_guessing():
function = functionSetter()
printInfo(function)
......@@ -24,13 +21,17 @@ def function_guessing():
def decode():
# Allows for 2 attemps in case the user mistypes
for j in range(2):
guess = input("Guess the password: ")
# TODO Remove this
if guess == "":
quit()
if guess == password:
print("CORRECT")
break
# Needs to make sure the user knows they have one more try or if they have just failed
else:
if j == 0:
print("One more try!")
......@@ -39,6 +40,9 @@ def decode():
def functionSetter():
'''Returns the randomly generated number to act as the function'''
# Needed to have 2 seperate random number generators for negative and positive numbers as it is not continuous
# And also needed to randomly choose if the function is negative or positive
negpos = random.randint(0, 1)
if negpos:
return random.randint(3, 23)
......@@ -47,18 +51,24 @@ def functionSetter():
def encode(function):
'''Returns a string that seems like a jumble of random letter'''
# Creating a list with the corresponding place in the alphabet of each letter in the word
nums_of_chars = []
for char in password:
num = ord(char) - 96
nums_of_chars.append(num)
# Creates a list, where each item is a letter in the now encoded word
encoded_list = []
for item in nums_of_chars:
# This ensures that when going above 26, it goes back to 1 again
# TODO Make sure this can also work when going into negative numbers
num = (item + function) % 26
if num == 0:
num = 26
encoded_list.append(chr(num + 96))
# converts the list of the encoded word to a string
encoded = ""
for item in encoded_list:
encoded += item
......@@ -67,12 +77,18 @@ def encode(function):
def printInfo(function):
# Maybe like actually use this function
'''
Information is printed, the word is also encoded here as each time the user fails to decode the word
it will change and be printed here
'''
print("Figure out the function to decode the password")
encoded_word = encode(function)
print("This is the encoded password: ", encoded_word)
def decoder_puzzle():
'''
Runs the decoding loop, currently allowing for an infinite amount of trys to guess the function
'''
correct = False
while not correct:
correct = function_guessing()
......@@ -80,4 +96,3 @@ def decoder_puzzle():
decode()
#decoder_puzzle()
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment