我有以下代码,输出如下。我想知道如何更改代码以获得没有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
如果letter
不在alpha
中,则alpha.find(letter)
返回-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