Here are the issues in your code:
- You always replace in the same block - overwriting get with the latest replacement operation result
- You don't check crossing list boundaries
- Remember p.1? It does not matter, because you exit after first iteration!
- You may end up replacing character many times - if you replace 'a' with 'c', then you'll replace 'c' with 'e', etc
- There are better ways to manage loop!
- There is module string that contains ascii_lowercase constant - well, that you may not have known
- Variable names - well, they are not very successful
from string import ascii_lowercase def fix(word, char_offset=2): new_word = '' for char_ in word: # Get index of replacement character and keep it below 26, so for "z" it will be "b" char_index = ascii_lowercase.find(char_) if char_index > -1: replacement_index = (char_index + char_offset) % 26 new_word += ascii_lowercase[replacement_index] else: new_word += char_ return new_wordPS I did not test the code - if there are some glitches, I leave fixing them to you as an excercise
Test everything in a Python shell (iPython, Azure Notebook, etc.)
- Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
- Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
- You posted a claim that something you did not test works? Be prepared to eat your hat.