2015-10-04 230 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