【发布时间】:2018-02-10 01:24:31
【问题描述】:
我正在尝试使用密码加密在 python 中对该函数进行编码然后解码。我写了这个函数。如何更改此功能以使其同时编码小写和大写字母?
def encrypt(message, key):
alphabet="abcdefghijklmnopqrstuvwxyz"
encMessage=""
for character in message:
if character in alphabet:
index = alphabet.find(character)
newPosition = (index+key)%26
newCharacter = alphabet[newPosition]
encMessage+=newCharacter
else:
encMessage+=character
return encMessage
【问题讨论】:
-
使用
character.lower()获取字母的小写版本 -
(index+key)%26不保证是字母数字字符,顺便说一句 -
@Barmar 在 for 循环中还是在 for 循环上方?
-
怎么会在for循环之上呢?
character变量未在此处定义。 -
这个函数真的是你写的吗?和this question一模一样
标签: python python-3.x caesar-cipher