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
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
codewordandencrypt. You then loop over encrypt for the third one. You only have one value foroldandnewso you perform the same operationlength of encrypttimes which will give you a single character or possibly an error.There's nothing more I can do since you haven't described the problem.