The function below will take a string(s) and rotate it a given amount(num). i.e cheer rotated by 7(cheer,7) will return jolly, and (melon, -10) will return cubed.
This is an attempt at exercise 8.10 from "How to think like a (Python) programmer, and it works just fine. The issue is I feel like there is a better way to write the ugly if..else statement, and possibly the whole function? Any pythonic suggestions??
Thanx in advance!
Lanier
def rotate_word(s,num):
rotate = num % 26
newword = ' '
word=s.upper()
for letter in word:
if ord(letter) + rotate > 90:
ascii_val = (ord(letter) + rotate) - 26
else:
ascii_val = ord(letter) + rotate
newword = newword + chr(ascii_val)
return newword