I get the error string index out of range inside of the encrypt function I don't know how to get rot to repeat over text. the code only works when both inputs are the same length. i want to keep the alphabet_position and the rotate_character functions the same if i can.
alpha_lower_list = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k",
"l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"]
alpha_upper_list = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K",
"L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"]
def alphabet_position(letter):
if letter in alpha_upper_list:
return alpha_upper_list.index(letter)
else:
return alpha_lower_list.index(letter)
def rotate_character(char, rot):
rotated_letter = ''
if char.isalpha():
rotate = alphabet_position(char) + rot
if rotate < 26:
if char in alpha_upper_list:
rotated_letter = alpha_upper_list[rotate]
return(rotated_letter)
else:
rotated_letter = alpha_lower_list[rotate]
return(rotated_letter)
else:
if char in alpha_upper_list:
rotated_letter = alpha_upper_list[rotate % 26]
return(rotated_letter)
else:
rotated_letter = alpha_lower_list[rotate % 26]
return(rotated_letter)
else:
return(char)
def encrypt(text, rot):
lis = []
for i in range(len(text)):
lis.append(rotate_character(text[i], alphabet_position(rot[i])))
return (''.join(lis))
def main():
user_text = input("Type a message: ")
rotate_by = input("Rotate by: ")
print(encrypt(user_text, rotate_by))
if __name__ == '__main__':
main()