2015-10-04 237 views
1

我一直在Python中工作,一直试图移动字母表中的所有字母n空格,但是我遇到了相当多的错误。Python:在字符串中移动字母表中的字母

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) 

到目前为止,它做什么,我需要做的,但是当我尝试运行此,它似乎也算位字母和我不知道如何将它们排除在外,同时保持他们在最后结果就像它们一样。

另一件我想知道的事情是,如果有什么办法让我不把字母变成小写字母,并保持代码的功能?

我一直在尝试几个小时的事情,但一直没能找到一个好的解决方案。

+0

你想改变的空间,以及或者只是改变信件并保留它们作为空间的空间? –

+0

我想改变这些字母并将它们作为空格保留。我将如何去存储空间的位置?我了解整个程序,但我不知道我在哪里开始编码。你能给我一个简单的例子吗? – Kei

+0

我正在研究一个解决方案,但它的正确形式可能如下所示: 开头: 保存列表中所有空格的索引位置 userInput = userInput.replace('','') and at结尾再次在这些索引位置添加空格。 –

回答

0

不编辑已有内容的一种方法是索引所有空间位置并将这些索引位置保存为字符串。请注意我的例子很混乱,你应该尝试实现没有尝试/除了结构。

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旨在增加理解,而不仅仅是提供解决方案。

+0

非常感谢!这正是我一直在寻找的东西,我是否正确地假设/理解这个代码可以调整为存储大写字母或任何字符的索引位置,以便我可以例如具有helLo世界2的userNumberInput变成jgnNq yqtnf而不是jgnnq yqtnf? – Kei

+0

你可以修改它来做到这一点,但它会更麻烦一点。 –

+0

我意识到在玩了一段时间之后。谢谢:) – Kei

0

您可以使用string模块和移动功能如下:

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号转移所有字母

+0

非常感谢!它做我需要它做的事情,在编码方面给予我相当多的关于其他选项的见解。 :) – Kei