0

So i have looked in this forum and grabbed a piece of code that let me encrypt text using a cypher. I have changed it so that you can input your own text to be encrypted and a little more.

I am 15 and this is my 2nd day learning python so if this is a easy fix for some ppl bear the last sentance in mind :)

message = input("Input: ")
key = 11
coded_message = ""

for ch in message:
    code_val  = ord(ch) + key
    if ch.isalpha():
        if code_val > ord('z'):
            code_val -= ord('z') - ord('a')
        coded_message = coded_message + chr(code_val)
    else:
        coded_message = coded_message + ch
# print ("Input: " + message)
print ("Output: " + coded_message)

My question is how do i make a code that will get the crypted text and turn it into the original text.

2
Contributors
2
Replies
5
Views
4 Years
Discussion Span
Last Post by calder.hutchins
1

You seem to have some bug, basically you decode with 26 - key to get original message. It is better to learn to use functions.

#Python 2 compatibility
from __future__ import print_function
try:
    input = raw_input
except:
    pass

def caesar(message, key):
    coded_message = ""

    for ch in message.lower():
        code_val  = (ord(ch) - ord('a') + key) % 26
        if ch.isalpha():
            coded_message = coded_message + chr(code_val + ord('a'))
        else:
            coded_message = coded_message + ch
    return coded_message

# print ("Input: " + message)
message = input("Input: ")
coded = caesar(message, 11)
print ("Output: " + coded)
print("Decoded: " + caesar(coded, 26-11))
0

Ok thankyou, i learn by looking at code then figuring out what makes it tick then i try to replicate it.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.