def encrypt(plaintext, key):
if key == 0:
ciphertext = plaintext
return ciphertext
key %= 26
ciphertext = ""
for letter in plaintext:
if letter.isalpha():
if letter.isupper():
new_letter = chr((ord(letter) - 65 + key) % 26 + 65)
else:
new_letter = chr((ord(letter) - 97 + key) % 26 + 97)
ciphertext += new_letter
else:
ciphertext += letter
return encrypt(ciphertext, key - 1)
If I input "hello" and the key is 2, then I should get "jgnnq" but I get "khoor". The program only wirks if the key is 1. Does anyon know why this happens and how to fix it?
Because you replace the value when the recursive iterates
Output:
So if you want jgnnq. Why you create recursive?
Edited under your comment:
Output: