大家好:)我正在构建一个脚本,用简单的文本循环(ROT)对文本进行编码。 脚本效果很好,但我遇到了问题,它还会旋转所有符号,如[空格,!,?,。]我使用ascii表来执行此操作,我该怎么做才能避免旋转那种人物?
def rot13(input,key): #Function to code a text with caeser chyper.
if key > 25:
key = 25
elif key < 2:
key = 2
finaltext = ''
for letter in input:
num = ord(letter)
if (num + key) > 122: #If the final number is greater than 122..
x = (num + key) - 122
finaltext += chr(x + ord('a') - 1)
elif((num + key <= 122)):
finaltext += chr(num + key)
print(finaltext)
答案 0 :(得分:3)
在“旋转”你的角色之前,添加一张支票以查看它是否是字母数字:
if letter.isalpha():
# Do your thing
else:
finaltext += letter
答案 1 :(得分:1)
试试这个:
>>> import string
>>> letter = 'a'
>>> letter in string.letters
True
>>> letter = '.'
>>> letter in string.letters
False