首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >凯撒加密工具

凯撒加密工具
EN

Code Review用户
提问于 2015-10-10 15:06:45
回答 2查看 354关注 0票数 3

我是Python的新手,我写了一个叫做凯撒加密工具的工具。目前它只加密。它从用户那里获取一个纯文本和一个密钥,然后使用该密钥加密纯文本。如何改进这些代码?

代码语言:javascript
复制
# This is version 1.0 Caesar Encryption Tool and it will be improved.
# This tool is written by Mahmoud Naguib.
# My facebook account is at : https:\\www.facebook.com\naguibarea
# This is letters table.
lettersTable = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q',
            'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']
# This line takes the user's key it's 3 by default.
key = int(input('Enter your key : '))
# This line takes the user's plain text and converts it
#  to uppercase to search for it in the English alphabet.
plainText = input('Enter your plain text : ').upper()
for letter in range(len(plainText)):
    # If there is a letter which isn't alphabet like numbers,
    #  special characters and spaces it will be appended to the list.
    if plainText[letter] not in lettersTable:
        lettersTable.append(plainText[letter])
    # This line locates the plain letter's numeric value in other words its    position.
    position = lettersTable.index(plainText[letter])
    # This line adds the key entered by the user to the plain letter's
    #  numeric value to reach the new encrypted letter.
    step = position + key
    # if the encrypted letter's numeric value is greater than 25
    #  then its new numeric value is the old numeric valuu %
    #  the number of English alphabet letters.
    if step > 25:
        step %= 26
    # If position is greater than 25 that means it's not an alphabet
    #  value and it will be printed without adding the key.
    if position > 25:
        step = position
    # This line locates the encrypted letter's numeric value in other words
    #  its position and prints the encrypted letter.
    # end is a keyword argument to combine all letters together.
    print(lettersTable[step], end='')
EN

回答 2

Code Review用户

回答已采纳

发布于 2015-10-10 18:06:01

欢迎来到代码评审。

我很快就会看完这段代码。

  1. PEP 8。它实际上是python中编码的标准。通常,我们将变量命名为lower_case_with_underscore中的变量,以及用作CONSTANT_UNDERSCORE的任何常量。
  2. 使用内置的功能。您正在使用您的letters_table来确定该字符是否为字母表,请改用isalpha()。类似地,您可以使用ordchr函数,这将允许您保留大小写。使用所有这些函数将消除对letters_table的需求。
  3. 保持一致的评论。您的评论指出,key默认值为3。它不是,如果我将输入留空,它将引发错误。
  4. 让你的代码模块化。您可以轻松地将I/O逻辑与实现逻辑分开。(使用功能)

由于您表示希望自己改进此代码,我将避免发布重构版本的代码。(如果你想让我举个例子,我可以这么做)。

下面是一些您可以合并的更多改进:

  1. 允许用户在加密和解密模式之间进行选择。(对于函数,这将通过具有默认值的额外参数来完成)
  2. 其他加密逻辑,同时维护相同的I/O逻辑。(将帮助您理解功能模块的威力)

一些Python代码质量的技能在将来可能很方便:

  1. 编写有用的文档串 (参见链接中的示例)。而对于这个程序,你的评论是足够的,它是有用的技能。
  2. 学习编写一些断言测试,然后编写单元测试。
票数 3
EN

Code Review用户

发布于 2015-10-12 10:07:34

关于lettersTable的一些注释。另一个答案已经使用isalpha解决了,但是如果您想要对字符列表进行测试,那么就有更简单的方法了。你实际上可以很好地使用character in string .if plainText[letter] in "ABCDEFGHIJKLMNOPQRSTUVWXYZ"会工作的。在某些情况下,字符串可以被看作是可迭代的,这就是其中之一。但是,您也可以使用string模块获得一个字母表常量。

代码语言:javascript
复制
from string import ascii_uppercase as lettersTable

该行消除了在您的情况下定义列表或字符串的需要。

你的代码在评论中游刃有余。你不需要解释一切。例如:

代码语言:javascript
复制
# This is letters table.

是不言而喻的。Python的可读性很强,所以任何能够跟随您的脚本的人都会知道如何遵循变量名。超过2/3的脚本是注释,您实际上可以通过删除它们来提高其可读性:

代码语言:javascript
复制
for letter in range(len(plainText)):

    # Add non alphabet characters to the list.
    if plainText[letter] not in lettersTable:
        lettersTable.append(plainText[letter])

    position = lettersTable.index(plainText[letter])
    step = position + key
    if step > 25:
        step %= 26

    # If it's not an alphabet value print without the key
    if position > 25:
        step = position

    print(lettersTable[step], end='')

您注意到我留下的行,并不能真正解释代码的功能。文档和先验知识可以更好地做到这一点。尤其是当这里的大部分台词都不难理解的时候。相反,我会留下一些注释来解释更抽象的想法,比如为什么在lettersTable中添加以前不存在的字符。

您的评论没有解释的一件事是,如果要忽略非字母字符,为什么要添加这些字符?似乎你这么做是为了防止index抚养ValueErrors,如果是这样的话,使用字符串更好,因为你可以使用lettersTable.find(plainText[letter])str.find()返回字符的索引,否则它将返回-1。然后,您只需检查结果是否为-1,而不是将字母添加到lettersTable中只是作为例外。

代码语言:javascript
复制
    position = lettersTable.find(plainText[letter])
    # If it's not an alphabet value print without the key
    if position == -1:
        print (plainText[letter], end='')
    else:
        step = position + key
        if step > 25:
            step %= 26

此外,您还可以使用更好的for循环。在Python中,您可以直接循环字符串或列表的元素。这意味着您不再需要继续访问plainText的索引来获得该值,它将更加可读性更强。

代码语言:javascript
复制
for letter in plainText:

    position = lettersTable.find(letter)
    if position == -1:
        # If it's not an alphabet value print without the key
        print (letter, end='')
    else:
        step = position + key
        if step > 25:
            step %= 26

        print(lettersTable[step], end='')
票数 2
EN
页面原文内容由Code Review提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://codereview.stackexchange.com/questions/107135

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档