Jun-23-2018, 05:08 PM
As a simple project I wanted to make a Caeser cipher encoder and decoder using my own knowledge. I got the encoding done but am stuck on the wrap around if you go above 26. At the title said 27 goes back to 1 or -1 goes back to 26. I have tried lots but cant get it to work. It works when you input a shift of about 0 - 4 or have no characters in it like a 'z'. But otherwise, I get a 'KeyErrror' ranging from about 27 - 30.
Here is my code. Please note: this may be just generally bad code / bad way of doing the conversion, but I would appreciate it if you did not improve it since I wanted to use as much of my own knowledge as possible and not have to use forums. Also there may be variable in there that don't do anything, they were just used for previous testing or conversion methods.
Dream.
Here is my code. Please note: this may be just generally bad code / bad way of doing the conversion, but I would appreciate it if you did not improve it since I wanted to use as much of my own knowledge as possible and not have to use forums. Also there may be variable in there that don't do anything, they were just used for previous testing or conversion methods.
from __future__ import print_function import time import string import operator num = 0 convto = {"a" : 1, "b" : 2, "c" : 3, "d" : 4, "e" : 5, "f" : 6, "g" : 7, "h" : 8, "i" : 9, "j" : 10, "k" : 11, "l" : 12, "m" : 13 , "n": 14, "o" : 15, "p": 16, "q" : 17, "r" : 18, "s" : 19, "t" : 20, "u" : 21, "v" : 22, "w" : 23, "x" : 24, "y" : 25, "z" : 26} #a = 0, b = 1 ect. convfrom = {"1" : "a", "2" : "b", "3" : "c", "4" : "d", "5" : "e", "6" : "f", "7" : "g", "8" : "h", "9" : "i", "10" : "j", "11" : "k", "12" : "l", "13" : "m" , "14" : "n", "15" : "o", "16" : "p", "17" : "q", "18" : "r", "19" : "s", "20" : "t", "21" : "u", "22" : "v", "23" : "w", "24" : "x", "25" : "y", "26" : "z"} usercipher = [] caeserints = [] def main(): global cipher global shift print("This will decrypt/encrypt Caeser Ciphers automatically!") time.sleep(0.5) print("----------------------------------------------------") time.sleep(1) cipher = input("Input cipher: ") cipher = cipher.replace(" ", "") cipher = cipher.lower() shift = int(input("Input the shift: ")) encrypt() def encrypt(): global encrypt global num usercipher = list(cipher) for letter in usercipher: numlist = convto[letter] + shift numlist = str(numlist) caeserints.append(numlist) for i in range(0,len(caeserints)): if oversize >= str(27): num = 0 num += 1 charsout = convfrom[caeserints[num]] #print(charsout) if __name__ == '__main__': main()Thanks in advance,
Dream.