题解 | #字符串加密#
字符串加密
http://www.nowcoder.com/practice/e4af1fe682b54459b2a211df91a91cf3
注意s.index('某字符'):获取某字符在该字符串中的索引值
while True:
try:
alpha='ABCDEFGHIJKLMNOPQRSTUVWXYZ'#模板字母表
key=input().upper()#将密匙转为大写
s=input()#输入要加密的字符串
new_key=''
nn_key=''
for i in key:#剔除重复的字母
if i not in new_key:
new_key +=i
for i in alpha:
if i not in new_key and len(new_key)<=26: # 用字母表剔除已有字母后补齐为26个新字母表
new_key +=i
for i in s:
if i.isupper():#要加密的字符是大写字母
nn_key +=new_key[alpha.index(i)]#先得到该字符在字母模板中的索引值,再根据索引值对应到密匙的字符
else:#如果是小写字母,先转为大写字母
nn_key +=new_key[alpha.index(i.upper())].lower()
print(nn_key)
except:
break