我尝试为只包含大写字母、下划线和句点的字符串实现一个类似于Caesar密码的编码方案。使用字母顺序进行旋转: biao jijklmnopqrstufwxyz\。在
迄今为止,Mycode:
def rrot(rot, s):
'returns reverse rotation by rot of s'
res = ''
for c in s:
x = ord(c)
res += chr(x + rot)
copy = res[::-1]
return copy
输出示例如下:
^{pr2}$但是当运行时,它会在整个字母表中运行,包括符号{[/etc)。我可以正确地移动字母的数量,但是会得到不需要的符号。我的错误输出:
^{3}$但这是正确的:
>>> rrot(1, 'ABCD')
'EDCB'
如何让它只遵循字符“ABCDEFGHIJKLMNOPQRSTUVWXYZ”的字母顺序?在
您的代码可以简单地修复:
您可以通过替换以下内容来修复它以使用其他字符顺序:
^{pr2}$说:
考虑:
用那些不好的例子:
这些例子的结果如下:
另一种方法是建立一个翻译表,如:
一旦超出循环,然后使用:
像这样:
结果是一样的:
相关问题 更多 >
编程相关推荐