Oшибка list index out of range. После версии 3.9 код работает некорректно
alphabet = ["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"]
direction = input("type 'encode' to encrypt, type 'decode' to decrypt:\n")
text = input("write your message:\n").lower()
shift = int(input("type the shift number:\n"))
def encrypt(plain_text,shift_amount):
cipher_text = ""
for letter in alphabet:
position = alphabet.index(letter)
new_position = position + shift_amount
new_letter = alphabet[new_position] # проблема тут !
cipher_text += new_letter
print(f"the encoded text is {cipher_text}")
encrypt(plain_text=text,shift_amount=shift)
Ответы (1 шт):
Автор решения: Danis
→ Ссылка
Надо итерировать не по алфавиту, а по слову
for letter in plain_text:
position + shift_amount
может получится число больше 26,поэтому надо взять остаток от деления
(position + shift_amount) % 26