I am trying to make a Ceasar Cipher. Struggling to get the ord() then shift it and then chr() for the encryption. I can get the first character in 'abc' and get an output of 'b' but not the whole string 'bcd'.
I feel it has to do with the return or not meeting the condition of going through the whole range, but I don't know how to troubleshoot. Any help would be fantastic.
Thanks in advance.
def cCipher(text,shift):
#First identify whether text is a string or note.
if type(text) != str:
return "The input is not a string"
#Get the ord() for each letter in string and shift
else:
for i in range(len(text)):
code = chr(ord(text[i])+shift)
return code
#This will be my test functions
print(cCipher('abc', 1))
You are terminating the function by calling return code
.
Try this instead:
def cCipher(text,shift):
#First identify whether text is a string or note.
if type(text) != str:
return "The input is not a string"
#Get the ord() for each letter in string and shift
else:
code = ""
for i in range(len(text)):
code += chr(ord(text[i])+shift)
return code
#This will be my test functions
print(cCipher('abc', 1))
Please note that there are other deficiencies in this code. The revised version I posted above fixes the issue you are experiencing but does not address any other issues.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.