Python编程;使用zip()方法编写Caesar密码
我有一个家庭作业问题:使用密码加密消息。我需要能够让用户输入一个数字来转换加密。例如,移位4会将“A”变为“E”。用户还需要输入要翻译的字符串。书中说使用zip函数来解决这个问题。我不知道那会怎样 我有这个,但它没有任何作用:Python编程;使用zip()方法编写Caesar密码,python,Python,我有一个家庭作业问题:使用密码加密消息。我需要能够让用户输入一个数字来转换加密。例如,移位4会将“A”变为“E”。用户还需要输入要翻译的字符串。书中说使用zip函数来解决这个问题。我不知道那会怎样 我有这个,但它没有任何作用: def caesarCipher(string, shift): strings = ['abc', 'def'] shifts = [2,3] for string, shift in zip(strings, shifts):
def caesarCipher(string, shift):
strings = ['abc', 'def']
shifts = [2,3]
for string, shift in zip(strings, shifts):
# do something?
print caesarCipher('hello world', 1)
zip是Python的一个内置函数,而不是问题标题所暗示的某种类型的方法
>>> help(zip)
Help on built-in function zip in module __builtin__:
zip(...)
zip(seq1 [, seq2 [...]]) -> [(seq1[0], seq2[0] ...), (...)]
Return a list of tuples, where each tuple contains the i-th element
from each of the argument sequences. The returned list is truncated
in length to the length of the shortest argument sequence.
>>>
您可以使用zip构建查找表字典,并使用字典对文本进行加密
from string import ascii_lowercase as alphabet
def cipher(plaintext, shift):
# Build a lookup table between the alphabet and the shifted alphabet.
table = dict(zip(alphabet, alphabet[shift:] + alphabet[0:shift]))
# Convert each character to its shifted equivalent.
# N.B. This doesn't handle non-alphabetic characters
return ''.join(table[c] for c in plaintext.lower())
感谢您将标签作为家庭作业!:好吧你希望你的代码做什么?不是答案,而是一个格式良好的注释我似乎无法对问题本身进行注释。我想我需要更多的分数?哦,也许吧。那我就投你的票!我还高估了你的另一个答案:PAwesome正是我想要的谢谢。