JES Cipher删除空格和标点符号

时间:2018-09-20 21:46:57

标签: python jes

我有以下代码,输出如下。我想知道应该如何更改代码才能在其中没有z的地方得到答案。换句话说,我需要它忽略空格/标点符号,以便最终输出改为sdfqfqeshqs

def buildCipher(key):
 alpha = "abcdefghijklmnopqrstuvwxyz"
 rest = ""
 for letter in alpha:
  if not (letter in key):
   rest = rest + letter
 print key + rest

def encode2(string, alpha2):
 alpha = "abcdefghijklmnopqrstuvwxyz"
 secret = ""
 for letter in string:
  index = alpha.find(letter)
  secret = secret+alpha2[index]
 print secret

buildCipher("earth")产生earthbcdfgijklmnopqsuvwxyz

encode2('this is a test', "earthbcdfgijklmnopqsuvwxyz")产生sdfqzfqzezshqs

1 个答案:

答案 0 :(得分:0)

如果alpha.find(letter)不在letter中,

alpha返回-1。 alpha2[-1]alpha2中的最后一个字母。因此,如果索引为-1的字母就可以跳过。像这样:

def encode2(string, alpha2):
    alpha = "abcdefghijklmnopqrstuvwxyz"
    secret = ""
    for letter in string:
        index = alpha.find(letter)
        if index != -1:
            secret = secret + alpha2[index]
    print secret