Help on Coded Correspondence (Python)

Hi all,

I’m a bit stuck on my first project (though still feeling pretty decent given that this is my 2nd week of learning programming from 0!):

I initially coded a method that worked well for steps 1 - 3 of this project, but would be stupid to try to use for cycling through all 26 possible offsets in step 4:

def code_10left(code): num_code = [] for i in code: num_code.append(ord(i)) num_code_shifted = [] for i in num_code: if i >= 107 and i <= 122: num_code_shifted.append(i - 10) elif i >= 97 and i <= 106: num_code_shifted.append(i + 16) else: num_code_shifted.append(i) coded_msg = [] for i in num_code_shifted: coded_msg.append(chr(i)) print("".join(coded_msg)) #code_10left(decoded_msg) def decode_10left(code): num_code = [] for i in code: num_code.append(ord(i)) num_code_shifted = [] for i in num_code: if i >= 97 and i <= 112: num_code_shifted.append(i + 10) elif i >= 113 and i <= 122: num_code_shifted.append(i - 16) else: num_code_shifted.append(i) decoded_msg = [] for i in num_code_shifted: decoded_msg.append(chr(i)) print("".join(decoded_msg)) #decode_10left(code)

So I restarted with a concept that I think should work for auto-looping:

alphabet = [] for i in range(97, 123): alphabet.append(chr(i)) #print(alphabet) def shift(offset): shifted_alphabet = [] for i in range(26): shifted_alphabet.append(alphabet[(i + offset) % 26]) return shifted_alphabet msg = "abcdefghijklmnopqrstuvwxyz" for i in range(26): for j in range(26): coded = msg.replace(alphabet[i], shift(1)[i]) print(coded) # def brute_force_decode(msg): # for i in range(26): # shift(i) # for j in range(26): # decoded = msg.replace(alphabet[j], shift(i)[j]) # print(str(i) + ": " + decoded)

My idea is to create a shifted alphabet (offset by 1 - 26) and replace each letter of the message by the shifted alphabet, and then cycle through the 26 possible offsets.

It does not work. I experimented with “abcdefghijklmnopqrstuvwxyz” as the coded message, and saw that my function is implicitly looping within itself, ie. if “d” gets shifted to “h”, then that same “h” gets re-shifted to “???” and it keeps going on like that.

Is there a way to fix this through my basic idea of replacing each letter by a shifted alphabet, or is this a dead end?

Hi there,
I think your idea might be workable. Can you link to the project you’re working on and also maybe give an example of how your method might look?

I think you might get some insight from indenting line 16 in the second version and running it. It should show you a bit better what’s going on under the hood.

After I get the project link and example, I think I can provide better guidance.


Your code after your restarted does work, but not in the way you think. If you look at the output after this runs, you’ll notice that every printed line shifts one letter of the message “msg” a time. So the first line is “bbcde…” and the second one is “accde…” and the third one is “abdde…” and so on. That’ because for each “i” in your code, the message “msg” remains constant while the print “coded” replaces only one letter.

Also, the “j” for loop does nothing so you can completely get rid of it.

BUT, what you are attempting to do ultimately won’t work because if you shift a letter to another letter, then shift that other letter as well, then the first letter shifts twice. There is a way to do what you are attempting, but it’s more complicated than you think and I can’t see the output being easier or better than trying to do this challenge the typical way.

In my opinion, you’d be better off starting over and thinking of this challenge from the perspective of replacing each letter in the message string individually and concatenating that letter to a new sting. IE if the secret message was “hey text”, your code should shift the letter “h” first and add that to a new string, then shift the “e” and add that to the new string. Also, this route will help you with the second half of this code challenge.

Best of luck.