Hi guys, so I have this exercise, outside of codeacademy and I’m struggling really hard to get it right, if someone would like to help me I’d be very glad, so here it is:
Create a function to encrypt a message. Your function must displace each alphabet letter according to a number. For example, if the number is 3 then “A” becomes “D” and “B” becomes “E”. For that, you can use the ASCII codification, using the ord() and chr() functions.
Observations: whitespaces and special characters must not be modified. Also any displacement in the letter “Z” or “z” with the number 3, for example, must be turned into “c” or “C”.
This is my code:
def cripto(msg, key):
code = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z',
'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z']
string = []
i = 0
while i < len(msg):
if (msg[i] in code):
number = int(ord(msg[i]))
character = chr(number + key)
string.append(character)
else:
letter = msg[i]
string.append(letter)
i += 1
stri = ""
strnova = stri.join(string)
return(strnova)
print(cripto('Computer Programming', 7))
the output was this: Jvtw|{ly Wyvnyhttpun
Which is wrong because | and { shouldn’t be there, the program only wants the alphabet letters.