我需要编写一个递归函数来加密消息 在python中将所有小写字符转换为下一个字符(z转换为a)。 到目前为止,这是我的代码,但我不知道如何更进一步,或者如何纠正错误。
sentence = input("Enter a message: \n")
letter_number = 0
def encrypt_sentence (s, number):
if letter_number == len(sentence) - 1:
return(s)
else:
if s[letter_number] == chr(122):
return encrypt_sentence(chr(ord(s[letter_number])-25), letter_number + 1)
else:
return encrypt_sentence(chr(ord(s[letter_number])+1), letter_number + 1)
print("Encrypted message")
print(encrypt_sentence(sentence, letter_number))
答案 0 :(得分:0)
我已修复您的代码,现在可以正常运行。
sentence = input("Enter a message: \n")
letter_number = 0
def encrypt_sentence (sentence):
if sentence:
if sentence == chr(122):
return chr(ord(sentence[letter_number])-25)
else:
return chr(ord(sentence[letter_number])+1)
print("Encrypted message")
ris = ''
for word in sentence:
ris += encrypt_sentence(word)
print(ris)