+ 1

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?

20th Dec 2018, 12:08 AM
DrChicken24
DrChicken24 - avatar
7 Answers
+ 3
Every 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
20th Dec 2018, 8:20 AM
HonFu
HonFu - avatar
+ 3
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.
20th Dec 2018, 5:45 PM
HonFu
HonFu - avatar
+ 2
What sort of encoding are you going for?
20th Dec 2018, 12:29 AM
HonFu
HonFu - avatar
+ 2
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?
20th Dec 2018, 1:26 AM
HonFu
HonFu - avatar
+ 2
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.
20th Dec 2018, 6:57 AM
DrChicken24
DrChicken24 - avatar
+ 1
Ok, thanks. In your code, what does inp do exactly?
20th Dec 2018, 5:42 PM
DrChicken24
DrChicken24 - avatar
0
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"?
20th Dec 2018, 1:05 AM
DrChicken24
DrChicken24 - avatar