+ 1
Basic Python Encrypt Function
Hi, I am trying to write a Python function that accepts a string and return a new string where all characters have been moved "up" two spots in the alphabet. For example, "a" will become "c". My code below does'nt work, not even sure it's right. def encrypt_message(string): alphabet = "abcdefghijklmnopkrstuvwxyz" newString = "" for index, char in enumerate(string): if string[index] == alphabet[index]: newString += alphabet[index + 2] return return newString #test cases: # encrypt_message("abc") should give "cde" # encrypt_message("xyz") should give "zab" # encrypt_message("") should give ""
3 Answers
+ 2
Only needed to add to the alphabet at the end two letters 'ab'
def encrypt_message(string):
alphabet = "abcdefghijklmnopkrstuvwxyzab"
newString = ""
for char in string:
newpos = alphabet.index(char)+2
newString += alphabet[newpos]
return newString
https://code.sololearn.com/cjP77OSIPzwt/?ref=app
+ 1
Thanks Vitaly Sokol and Choe for your inputs.