【发布时间】:2022-01-22 21:51:54
【问题描述】:
我正在尝试为 Caesar Cypher 创建一个函数,该函数使用 ord() 函数将任何字符串转换为其各自的 unicode,然后将 unicode 移动两步。
例如,字符串“a”的 unicode 是整数 97。
print(ord('a'))
之后,这个移位的 unicode 被转换回其各自的字符,以产生一段难以理解的代码。
。回溯(最近一次通话最后): 文件“main.py”,第 11 行,在 密码(味精) 文件“main.py”,第 9 行,在 Ccypher 中 a = a + str(chr(lst[i])) UnboundLocalError:在赋值之前引用了局部变量“a”**
我尝试通过添加将 a 转换为全局变量
global a 在函数体中,但我没有输出,只是空白。
我写的代码如下:
lst = list()
a = ''
msg = "Meet me at the Ritz Carlton at 9 o'clock, don't be late!" #message to encrypt
def Ccypher(string, shift = 2):
for i in range(len(msg)):
lst.append(ord(msg[i]) + shift)
a = a + str(chr(lst[i]))
return a
Ccypher(msg)
【问题讨论】:
-
这不是真正的凯撒,因为您会将任何字符翻译成另一个(可能不可打印或当前编解码器中不存在)一个,而 官方 凯撒密码将字母转换为字母.是否是一个问题取决于你......
标签: python encryption unicode cypher list-manipulation