I'm new to python and struggling quite a bit to get this going. This is my task:
The Six-Letter Cipher is a method of encoding a secret message that involves both substitution and transposition. The encryption starts by randomly filling a 6 6 grid with the alphabet letters from A to Z and the digits from 0 to 9 (36 symbols in total). This grid must be known to both the sender and receiver of the message. The rows and columns of the grid are labelled with the letters A, B, C, D, E, F.
Write a Python program that implements the six-letter cipher method. Your program should: 1. Create a 6x6 grid and fill it randomly with letters and numbers as described in the first paragraph, then prompt the user to enter a secret message. 2. Display the 6x6 grid and the generated ciphertext, after the user enters the secret message. 3. Prompt the user to enter the ciphertext to display the original message. It is OK to ask the user to separate every two letters of the ciphertext with a space or comma.
The bit I am struggling with is how do I search through the nested lists for the randomly placed letter that has been entered and give the coordinates. Also won't the coordinates be given in numbers i.e. 0,1, rather than letters i.e. A,B I think I could manage the encoding and decoding once I have the ideas of how to use this nested list.
Here is my code so far:
grid = [["Z","9","G","Q","T","3"],
["Y","8","F","P","S","2"],
["X","7","E","O","R","1"],
["W","6","D","N","M","0"],
["V","5","C","L","K","U"],
["J","4","B","I","H","A"]]
def main():
print("Welcome to the sixth cipher")
print("Would you like to (E)Encode or (D)Decode a message?")
operation = input()
if(operation == "E"):
encodecipher()
elif(operation == "D"):
decodecipher()
else:
print("Sorry, input not recognised")
def encodecipher():
print("Please enter a message to encode:")
messagetoencode = input()
def decodecipher():
print("Decode Test")
rowcolumn()
def rowcolumn():
pass
main()
This is a simple way to get the co-ordinates of a character in the grid :
I realise you're doing this for learning/homework purposes, but I'll point this out for something for you to bear in mind later. It's easier to think of what you have as something of 36-items (that just happens to be representable as a 6x6) - or more explicity - you have a character -> coord, and a
dict
is very useful to do this, and reverse it...You can use Python's enumerate to iterate over values and provide an index position of each value as you go:
You can adapt this into your chosen function structure, such as the following (if values in your grid are unique):
As this will raise a
ValueError
if the value isn't found, you'll have to handle this in your calling code.If you then need to map between 0-indexed row and column numbers to the letters A...F you can do something like:
Which will give you the following:
Putting it all together gives you: