Python Forum
Wrap from end to beginning. 27 to 1, 28 to 2 etc
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Wrap from end to beginning. 27 to 1, 28 to 2 etc
#1
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.

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.
Reply


Messages In This Thread
Wrap from end to beginning. 27 to 1, 28 to 2 etc - by DreamingInsanity - Jun-23-2018, 05:08 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Just beginning dalekeel 2 431 Apr-15-2024, 11:49 AM
Last Post: Pedroski55
  Program doesnt return beginning bilisim19 2 1,022 Feb-15-2023, 06:23 PM
Last Post: Larz60+
  List of dataframe values beginning with x,y or z glidecode 3 2,033 Nov-08-2021, 10:16 PM
Last Post: glidecode
  How to start the program from the beginning. iamaghost 5 3,146 Feb-23-2021, 03:40 AM
Last Post: deanhystad
  Python Dev suggestion: Combining line-wrap with comments inside functions NikoNiels 2 1,822 Sep-26-2020, 07:45 PM
Last Post: bowlofred
  want to change the beginning of the result Rudinirudini 5 3,735 Nov-15-2018, 11:28 AM
Last Post: Rudinirudini
  Beginning of Beginner Help: Should I start with Python? appdevelnewb 2 3,170 Jul-23-2018, 11:17 PM
Last Post: appdevelnewb
  How to Loop CSV File Beginning at Specific Row? bmccollum 5 40,570 Nov-05-2017, 11:04 PM
Last Post: bmccollum
  Need help for beginning coding sylas 13 8,555 Mar-28-2017, 06:36 AM
Last Post: sylas

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020