【发布时间】:2022-01-04 01:29:50
【问题描述】:
我正在尝试为我制作的密码制作一个编码程序,但我无法连接字符串,这是我的代码:
charToBin = {
"a":1,
"b":10,
"c":11,
"d":100,
"e":101,
"f":110,
"g":111,
"h":1000
}
binToWrd = {
1:"Intel",
10:"Info",
11:"Indipendent",
101:"Imposibble",
100:"Info-stolen",
110:"Indian-Ogres",
111:"Initially",
1000:"Infant-Orphan-Ogre-Ogle"
}
endTxt = " "
cipher = " "
def txtToBin():
txt = input(":")
txtArray = txt.split(" ")
for x in range(len(txt)):
endTxt += str(charToBin[txtArray[x]])+" "
print(endTxt)
def binToCip():
codeTxtArr = endTxt.split(" ")
for x1 in codeTxtArr:
for x2 in binToWrd:
cipher =+ x1.replace(str(x2), binToWrd[x2])
print(cipher)
txtToBin()
binToCip()
它返回了这个错误:
Traceback (most recent call last):
File "main.py", line 40, in <module>
txtToBin()
File "main.py", line 30, in txtToBin
endTxt = endTxt + str(charToBin[txtArray[x]])+" "
UnboundLocalError: local variable 'endTxt' referenced before assignment
谁能解释为什么会发生这种情况以及如何解决它?
【问题讨论】:
标签: python string encryption concatenation encode