简体   繁体   English

如何将我的代码转换为可正常使用的Caesar密码翻译器?

[英]How do I turn my code into a working Caesar Cipher translator?

I've been trying to work on this code of mine that takes a string of words, converts every letter into an ASCII code, adds a certain number (rotate_number), and then prints out the translated version of those numbers, all converted back into a string of random letters (but with the spaces and symbols remaining the same). 我一直在尝试使用我的这个代码,它使用一串单词,将每个字母转换为ASCII代码,添加一定的数字(rotate_number),然后打印出这些数字的翻译版本,所有这些都转换回一串随机字母(但空格和符号保持不变)。 For example, I want the string of "How are you today?" 例如,我想要字符串“今天好吗?” to rotate by a number of say +4, so that the final product says, "Lsa evi csy xshec?" 旋转数个+4,以便最终产品显示“ Lsa evi csy xshec?”

Here's the code I have so far, however when I run it, only one letter is rotated and printed out: 这是我到目前为止的代码,但是当我运行它时,只有一个字母被旋转并打印出来:

def encrypt_words(words, rotate_number):
    blank = ""
    for i in words:
        translate1 = ord(i) + rotate_number
        translated = chr(translate1)
        blank += translated
    print translated

encrypt_words("How are you today?", 3)

Also, I am sorry if my question has already been answered. 另外,如果我的问题已经得到回答,我也很抱歉。 I've been searching all across the web and this specific site, but nothing seems to work for me... 我一直在整个网络和这个特定站点中进行搜索,但是似乎没有任何工作对我有利...

It looks like you want to be printing the string "blank", not translated as in your code. 看来您要打印字符串“ blank”,而不是按照代码中的含义进行翻译。 Try the following: 请尝试以下操作:

def encrypt_words(words, rotate_number):
    blank = ""
    for i in words:
        translate1 = ord(i) + rotate_number
        translated = chr(translate1)
        blank += translated
    print blank

Blank is the concatenation of each of the translated values. 空白是每个转换值的串联。 You should probably rename 'blank' as the name is misleading. 您可能应该重命名“空白”,因为该名称具有误导性。

I would recommend first creating a rotate method 我建议先创建一个旋转方法

def rotate(alphabet,rotation):
    return alphabet[rotation:]+alphabet[:rotation]

#test it
print(rotate(string.ascii_lowercase,5))

now create a lookup table of the rotation 现在创建旋转的查找表

def create_lookup_table(rotation,*alphabets):
    if not alphabets:
       alphabets = [string.ascii_lowercase,string.ascii_uppercase]
    alphabet = "".join(alphabets)
    rotated = "".join(rotate(a,rotation) for a in alphabets)
    return dict(zip(alphabet,rotated))

# test it
create_lookup_table(5)
create_lookup_table(2,"abcdef","GHIJKLM")

now implementing ceasar cipher is trival because we have given ourselves the toolset to help us 现在实施ceasar密码很简单,因为我们已经为自己提供了帮助我们的工具集

def ceasar(msg_plain,rotation):
    lookup_table = create_lookup_table(rotation)
    return ''.join(lookup_table.get(char,char) for char in msg_plain)

# test it
ceasar("Hello World!",13) # 'Uryyb Jbeyq!'

You would want to do something like : 您可能想做些类似的事情:

def encrypt_words(words, rotate_number):
    blank = ""
    for i in words:
        if i.isalpha():  # if part of alphabet shift with cipher
            translate1 = ord(i) + rotate_number
            translated = chr(translate1)
            blank += translated
        else:  # else leave as is
            blank += i
    print blank  # print entire word, not just last character
    # change blank variable to something that better describes function?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM