Function That Receives and Rotates Character - Caesar Cipher

雨燕双飞 提交于 2019-12-04 15:27:03
def rotate(letter, rot):
    shift = 97 if letter.islower() else 65
    return chr((ord(letter) + rot - shift) % 26 + shift)

letter = input('Enter a letter: ')
rot = int(input('Enter a number: '))
print(rotate(letter, rot))

You can use the string module and then use the modulo operator to "wrap around" the end of the alphabet:

from string import lowercase

def rotate_char(char, rot):

    i = lowercase.index(char)
    return lowercase[(i + rot) % 25]
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!