How to make a cipher in python?
I've been trying, but im stuck on how to output their input in decoded fashion. Same issue with encoding a decoded input. Any ideas?
12/20/2018 12:08:56 AM
DrChicken24
7 Answers
New AnswerEvery character has an ordinal number (unicode position). You can get that with ord(). And you can turn a number into the corresponding letter with chr(). So one way would be manipulating this number. chr(97) leads to 'a', chr(98) to 'b'. So for example to switch a letter of a string against the next one in the alphabet, you can loop over the string, translate the letter to its number, increase that number, turn it back to a string, add it. for letter in str1: str2 += chr(ord(letter)+1) You could also put your letters in a list and work with the indexes. https://code.sololearn.com/cKQmoIQ3Ioqn/?ref=app
inp is just the variable that takes the user input in the format I describe in the initial comment. The input gets split directly into steps, direction and the actual sentence to be encoded. So it's a list with three elements.
a to b AND a to b? Apple to Bapple added a letter, not exchanged one. I still don't see what exactly you want to do. You first need to choose an encoding pattern, don't you?
Ah, sorry. Its a type and I meant apple to bpple. I just want to know how to output their input and replacing letters to the one that corresponds with the code.
For example, if I wanted to change a to b and a to b, etc., how would I go about changing the string, for example, "Apple" using the code to make it output "Bapple"?