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)))
for i in encrypt:
    out = ord(old) + ord(new) -96 
print(chr(out))

Above is my full code so far. However, if I enter 'hi' for both codeword and encrypt, all that is printed, is 'r'. Is anyone able to point out my mistake, as I have tried everything I can think of, I am fairly certain that my error lies within the for loop, But if I do 'for i in encrypt + codeword' it prints 'r' still. I never knew something so simple once explained, can hold so many difficulties.

Many thanks.

I have edited the code and shown it below, yet I am still struggling. I know there are many people out there who know this, but I am new, I have tried to research but I failed to find anything

codeword = input('Enter codeword : ')
codeword = codeword.lower().replace(" ", "")
old = codeword[-1]

encrypt = input('Enter text to encrypt : ')
encrypt = encrypt.lower().replace(" ", "")
new = encrypt[-1]
for i in new:
    print(chr(ord(old)+ ord(new)-96))

*I have once again done further editing and produced a program that does near enough what I want, yet there is a slight issue, it prints it twice, but if I get rid of the for loop I will prevent the program from printing anything at all.

codeword = input('Enter codeword : ')
codeword = codeword.lower().replace(" ", "")

encrypt = input('Enter text to encrypt : ')
encrypt = encrypt.lower().replace(" ", "")

for i in codeword+encrypt:
    print(chr(ord(i)+ ord(i)-96))

Unfortunately, if I put 'hi' as both the codeword and as encrypt, it prints, p r p r

1

There are 1 best solutions below

1
On BEST ANSWER

Your problem doesn't exist in the third for loop. It exists in the first two. You are setting old and new to the last character of codeword and encrypt. You then loop over encrypt for the third one. You only have one value for old and new so you perform the same operation length of encrypt times which will give you a single character or possibly an error.

codeword = input('Enter codeword : ')
codeword = codeword.lower().replace(" ", "")

old = codeword[-1]
print (old)

encrypt = input('Enter text to encrypt : ')
encrypt = encrypt.lower().replace(" ", "")

new = encrypt[-1]
print (new)

for i in encrypt:

    out = ord(old) + ord(new) - 96
    print (out, old, new, i) 
print(chr(out))

Enter codeword : Hello
o

Enter text to encrypt : Hello
o

126 o o h
126 o o e
126 o o l
126 o o l
126 o o o

~

There's nothing more I can do since you haven't described the problem.