Basic Python Encrypt Function | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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 ""

9th Mar 2020, 4:30 PM
hartechworld
hartechworld - avatar
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
9th Mar 2020, 6:10 PM
Vitaly Sokol
Vitaly Sokol - avatar
+ 1
Thanks Vitaly Sokol and Choe for your inputs.
10th Mar 2020, 11:39 AM
hartechworld
hartechworld - avatar