使用python的凯撒密码,可以使用一些帮助 [英] Caesar's Cipher using python, could use a little help

查看:45
本文介绍了使用python的凯撒密码,可以使用一些帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在使用 python 时制作凯撒密码"..这是我目前所拥有的.谁能告诉我这是怎么看的?我是否朝着正确的方向前进?我错过了什么?例如,当我运行该程序时(josh 很酷),我没有在同一行获得密码.当我执行 main(3)

I'm trying to make a "Caesar's Cipher" while using python..this is what I have so far. Could anyone tell me how this is looking? Am I going in the right direction? What am I missing? When I run the program to say for example (josh is cool) I don't get the cipher on the same line. It looks like this when I do main(3)

m
r
v
k
l
v
f
r
r
o

但是它将每个字母放在一个新行上.我怎么能做到这一点,让它在一行上?

But it puts each letter on a new line. How could I do it so that it is on one line?

def main(k):

    if k<0 or k>231:
        print "complaint"
        raise SystemExit

    Input = raw_input("Please enter Plaintext to Cipher")

    for x in range(len(Input)):
        letter=Input[x]
        if letter.islower():
            x=ord(letter)
            x=x+k
            if x>122:
                x=x-122+97
            print chr(x),
        if letter.isupper():
            x=ord(letter)
            x=x+k
            if x>90:
                x=x-90+65
            print chr(x),

推荐答案

我喜欢 kaizer.se 的答案,但我认为我可以使用 string.maketrans 函数:

I like kaizer.se's answer, but I think I can simplify it using the string.maketrans function:

import string

first = raw_input("Please enter Plaintext to Cipher: ")
k = int(raw_input("Please enter the shift: "))

shifted_lowercase = ascii_lowercase[k:] + ascii_lowercase[:k]

translation_table = maketrans(ascii_lowercase, shifted_lowercase)

print first.translate(translation_table)

这篇关于使用python的凯撒密码,可以使用一些帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆