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

时间:2015-10-04 16:51:26

标签: python string for-loop whitespace

我一直在使用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)

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

我想知道的另一件事是,我是否有办法让这些字母变成小写字母并使代码保持功能?

我一直在努力工作几个小时,但未能找到一个好的解决方案。

2 个答案:

答案 0 :(得分:0)

在不编辑内容的情况下执行此操作的一种方法是索引空格所在的所有位置,并将这些索引位置保存为字符串。请注意我的例子很乱,你应该尝试在没有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)

评论您是否对其工作原理有任何疑问,因为stackoverflow旨在增加理解,而不仅仅是提供解决方案。

答案 1 :(得分:0)

您可以按如下方式使用字符串模块和移位功能:

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])

功能取消更改空间位置并移动userInput中的所有字母,完全是userNumberInput数字