HJ21 简单密码

思路:

step1:输入明文n,创建字典dic(“A-Z”对应的“b-a”),创建“shuzi”字符串(“0-9”),创建空串x(便于输出密文)
step2:遍历明文n,如果i在“shuzi”内,则直接将数字加到x中;如果i在字典内,则将对应的字母加到x中;如果i是小写字母,则按照题目要求,将对应的数字字符加到x中;
step3:打印密文x

代码如下:

while True:
    try:
        n = input()
        dic={'A':'b','B':'c','C':'d','D':'e','E':'f','F':'g','G':'h','H':'i','I':'j','J':'k','K':'l','L':'m','M':'n','N':'o','O':'p','P':'q','Q':'r','R':'s','S':'t','T':'u','U':'v','V':'w','W':'x','X':'y','Y':'z','Z':'a'}
        shuzi = '0123456789'
        x = ''
        for i in n:
            if i in shuzi:
                x += i 
            elif i in dic:
                x += dic[i]
            else:
                if i in 'abc':
                    x += '2'
                elif i in 'def':
                    x += '3'
                elif i in 'ghi':
                    x += '4'
                elif i in 'jkl':
                    x += '5'
                elif i in 'mno':
                    x += '6'
                elif i in 'pqrs':
                    x += '7'
                elif i in 'tuv':
                    x += '8'
                elif i in 'wxyz':
                    x += '9'
        print(x)
    except:
        break