问题是一个已知的问题:给定一个句子,返回所有字母在字母表中被 1 转置的句子,但前提是字母是 ay。
我知道类似的问题已经在这里被问过很多次了,但是我在我的案例中应用的解决方案实际上来自这些 stackoverflow 答案之一,并且该函数仍然不断向前跳 2-3 个字母:
from string import ascii_letters
def inverter(sentence):
for x in sentence:
if x in ascii_letters and x!= 'z' and x != ' ':
sentence = sentence.replace(x,ascii_letters[ascii_letters.index(x)+1])
else:
sentence = sentence
return sentence
sent3 = 'a quick brown fox jumps over the lazy dog'
inverter(sent3)
输出:
'c uwkem cuqzq hqz kwnqu qwfu uif mczz eqh'
突变循环中可能出了什么问题?