Search code examples
pythonpython-3.xencryptionchrord

Adding the values of each chr(ord() character in a string?


codeword = input('Enter codeword : ')
codeword = codeword.lower().replace(" ", "")
for i in codeword:
    old = (Chr(ord(i)))

encrypt = input('Enter text to encrypt : ')
encrypt = encrypt.lower().replace(" ", "")
for i in encrypt:
    new = (Chr(ord(i)))

value = new + old
for i in value:
    print(Chr(ord(i)))

I am doing encryption and decryption for my GCSE computing, I already made a program that successfully encryptions text to the value of 5 letters in the alphabet ('a' would become 'f') the alphabet and then a program that decrypts it. However, I also have to write a program that adds the values of a codeword to the text and prints the new letter. So, for example, if the codeword was 'gcses' and the text 'hello' it would print o (7 + 8) h (3 + 5) e (19 + 12) q (5 + 12) h (19 + 15)

The code I have at the moment I believe is vaguely on the right track, however, I would like to know if it is possible to add the values of two ord() functions to carry out this program. Thanks. Any help would be much appreciated.


Solution

  • Easiest way to do this might be to add a lookup string containing the alphabet in order to find the letter numbers...

    alphabet = ' abcdefghijklmnopqrstuvwxyz'
    print alphabet.find('b')
    # prints '2'
    print alphabet[alphabet.find('g') + alphabet.find('h')]
    # prints 'o' as expected.
    

    You would need to handle overflow though (%26 would work)