繁体   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