0
我正在为学校做一个加密项目,并且遇到了将偏移因子添加到每个字符的相应ASCII码上的问题。我有一个数字列表(例如[[83],[100],[109]]),但其中的一些也有空格 - 例如[[83],[111],[109],[101] ,[98],[111],[100],[121],[''],[111],[110]]。我想为每个添加一个偏移因子,但我显然不能将它添加到空间中。我如何忽略它(因为我仍然希望它出现在列表中)?TypeError:“只能连接列表(而不是”int“)列表”添加一个数字到列表中的每个项目?
这里是我的代码:
def encrypttext():
ascii = []
for item in stringlist:
if item==" ":
ascii_item=[]
for char in item:
ascii_item.append(" ")
ascii.append(ascii_item)
continue
else:
ascii_item = []
for char in item:
ascii_item.append(ord(char))
ascii.append(ascii_item)
print(ascii)
for item in ascii: #Main problem from here downwards
if item==' ':
continue
else:
item=item+offsetfac
if item>126:
item=item-94
else:
continue
print(ascii)
错误:
Traceback (most recent call last):
File "C:\Users\jacqu_000\Downloads\ENCRYPTION PROJECT.py", line 100, in <module>
encryption=encrypttext()
File "C:\Users\jacqu_000\Downloads\ENCRYPTION PROJECT.py", line 71, in encrypttext
item=item+offsetfac
TypeError: can only concatenate list (not "int") to list