You can use something in Python called ord
to get the integer value and add that by one.
Here is the solution:
def encrypt(word):
encrypted = ""
for letter in word:
encrypted += chr(ord(letter) + 1)
return encrypted
And if you want to the inverse of ord
, then you can use chr
.
This would be the decrypt function:
def decrypt(word):
decrypted = ""
for letter in word:
decrypted += chr(ord(letter) - 1)
return decrypted
FYI, when a letter like "a" is used inside the ord
function, it returns 97
, and if we were to use the decrypt method on "a", then it would return " ` " which is not useful, but you can add a simple if
statement which fixes this issue.