我是Python的新手,我写了一个叫做凯撒加密工具的工具。目前它只加密。它从用户那里获取一个纯文本和一个密钥,然后使用该密钥加密纯文本。如何改进这些代码?
# 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='')发布于 2015-10-10 18:06:01
欢迎来到代码评审。
我很快就会看完这段代码。
letters_table来确定该字符是否为字母表,请改用isalpha()。类似地,您可以使用ord和chr函数,这将允许您保留大小写。使用所有这些函数将消除对letters_table的需求。key默认值为3。它不是,如果我将输入留空,它将引发错误。由于您表示希望自己改进此代码,我将避免发布重构版本的代码。(如果你想让我举个例子,我可以这么做)。
下面是一些您可以合并的更多改进:
一些Python代码质量的技能在将来可能很方便:
发布于 2015-10-12 10:07:34
关于lettersTable的一些注释。另一个答案已经使用isalpha解决了,但是如果您想要对字符列表进行测试,那么就有更简单的方法了。你实际上可以很好地使用character in string .if plainText[letter] in "ABCDEFGHIJKLMNOPQRSTUVWXYZ"会工作的。在某些情况下,字符串可以被看作是可迭代的,这就是其中之一。但是,您也可以使用string模块获得一个字母表常量。
from string import ascii_uppercase as lettersTable该行消除了在您的情况下定义列表或字符串的需要。
你的代码在评论中游刃有余。你不需要解释一切。例如:
# This is letters table.是不言而喻的。Python的可读性很强,所以任何能够跟随您的脚本的人都会知道如何遵循变量名。超过2/3的脚本是注释,您实际上可以通过删除它们来提高其可读性:
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中只是作为例外。
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的索引来获得该值,它将更加可读性更强。
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='')https://codereview.stackexchange.com/questions/107135
复制相似问题