"""This module contains code from Think Python by Allen B. Downey http://thinkpython.com Copyright 2012 Allen B. Downey License: GNU GPLv3 http://www.gnu.org/licenses/gpl.html """ import string def rotate_letter(letter, n): """Rotates a letter by n places. Does not change other chars. letter: single-letter string n: int Returns: single-letter string """ if letter.isupper(): start = ord('A') elif letter.islower(): start = ord('a') else: return letter c = ord(letter) - start i = (c + n) % 26 + start return chr(i) def rotate_word(word, n): """Rotates a word by n places. word: string n: integer Returns: string """ res = '' for letter in word: res += rotate_letter(letter, n) return res if __name__ == '__main__': print rotate_word('cheer', 7) print rotate_word('melon', -10) print rotate_word('sleep', 9)