簡體   English   中英

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)

如果您對它的工作方式有任何疑問,請發表評論,因為stackoverflow旨在增加理解,而不僅僅是提供解決方案。

您可以如下使用字符串模塊和shift函數:

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數字

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM