Python:在字符串中移动字母表中的字母

Python: Moving letters within the alphabet in a string

我一直在 Python 工作,并且一直在尝试将字符串中的所有字母移动到字母表中的 n 个空格,但是我 运行 遇到了很多错误。

print("Cryptography")
userInput = str(input("Write anything that you'd like to encrypt here: "))
userNumberInput = int(input("Please choose a number without decimals here: "))
userInput = userInput.lower()
wordCount = userInput.split()
loopRunner = int(0)
letterCount = list()
for word in range(len(wordCount)):
    letterCount.insert(loopRunner,len(wordCount[loopRunner]))
    print(letterCount[loopRunner])
    loopRunner = loopRunner + 1

outputString = "" .join((chr(97 + (ord(letter) -97 + userNumberInput) % 26) for letter in userInput))
loopRunner = 0
for word in range(len(wordCount)):
    outputString2 = [outputString[i:i+letterCount[loopRunner]] for i in range (0, len(outputString), letterCount[loopRunner])]
    loopRunner = loopRunner + 1
finalResult = " " .join(outputString2)
print(finalResult)

到目前为止,它完成了我需要它做的事情,但是当我尝试 运行 时,它似乎也将空格计为字母,我不知道如何排除它们,同时将它们保留在最终结果就是这样。

我想知道的另一件事是,我是否有任何方法可以防止字母变成小写字母并保持代码正常运行?

我已经尝试了几个小时,但一直未能找到好的解决方案。

在不编辑现有内容的情况下执行此操作的一种方法是索引所有空格所在的位置并将这些索引位置保存为字符串。请注意我的示例很混乱,您应该尝试在没有 try/except 结构的情况下实现。

n = 0
spacesLocation = []
while(True):
    try:
        spacesLocation.append(userInput.index(' '))
        userInput = userInput.replace(' ', '', 1)
        n+=1
    except:
        n = 0
        break

然后在最后将它们添加回:

while(n < len(spacesLocation)):#goes through where all the spaces are
    finalResult = finalResult[:spacesLocation[n]+n] + ' ' +finalResult[spacesLocation[n]+n:]#adds spaces where they go
    n+=1#re-iterates to go through where all the spaces should be

所以你的整个事情看起来像这样:

print("Cryptography")
userInput = str(input("Write anything that you'd like to encrypt here: "))
n = 0
spacesLocation = []
while(True):
    try:
        spacesLocation.append(userInput.index(' '))
        userInput = userInput.replace(' ', '', 1)
        n+=1
    except:
        n = 0
        break
userNumberInput = int(input("Please choose a number without decimals here: "))
userInput = userInput.lower()
wordCount = userInput.split()
loopRunner = int(0)
letterCount = list()
for word in range(len(wordCount)):
    letterCount.insert(loopRunner,len(wordCount[loopRunner]))
    print(letterCount[loopRunner])
    loopRunner = loopRunner + 1

outputString = "" .join((chr(97 + (ord(letter) -97 + userNumberInput) % 26) for letter in userInput))
loopRunner = 0
for word in range(len(wordCount)):
    outputString2 = [outputString[i:i+letterCount[loopRunner]] for i in range (0, len(outputString), letterCount[loopRunner])]
    loopRunner = loopRunner + 1
finalResult = " " .join(outputString2)
while(n < len(spacesLocation)):
    finalResult = finalResult[:spacesLocation[n]+n] + ' ' +finalResult[spacesLocation[n]+n:]
    n+=1
print(finalResult)

如果您对它的工作原理有任何疑问,请发表评论,因为 Whosebug 旨在增加理解,而不仅仅是提供解决方案。

您可以使用字符串模块和移位函数如下:

import string

def shift():
    userInput = input("Write anything that you'd like to encrypt here: ")
    userNumberInput = int(input("Please choose a number without decimals here: "))
    letters=' '+string.ascii_letters
    if userNumberInput > len(letters): userNumberInput = userNumberInput%len(letters)
    d={k:v for k,v in zip(letters,' '+letters[userNumberInput:]+letters[:userNumberInput])}
    return ''.join([x.replace(x,d[x]) for x in userInput])

函数不变 space 位置并移动 userInput 中的所有字母正好 userNumberInput number