[英]Why am I getting ('str' function is not callable) error even though I haven't used any 'str' named variable?
originalMSG = ['1','2','3','4','5','6','7','8','9','0','a','b','c','d','e','f','g','h','i','j','k','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z',' ']
codedMSG = ['v', 'u', '0', 's', '1', 'r', 'o', 'x', 'a', 'p', 'y', 'd', 'k', 'b', 'z', 'g', 'q', 'l', '5', ' ', 'w', 'c', 'm', '8', 'h', '4', 't', '3', '6', 'k', 'n', '7', 'j', '2', '9', 'e', 'i', 'f']
str2 = 'wello'
h=[]
#print(str2.index('w'))
for i in range(0, len(str2)):
h=codedMSG(originalMSG.index(str2(i)))
print(str2)
It is giving me error in 8th line TypeError: 'str' object is not callable.它在第 8 行 TypeError 中给了我错误:'str' object is not callable。 in line 7
在第 7 行
Replace str2(i)
with str2[i]
- indexing rather than calling用
str2[i]
替换str2(i)
- 索引而不是调用
Also, do the same with codedMSG(...)
- replace with codedMSG[...]
此外,对
codedMSG(...)
执行相同操作 - 替换为codedMSG[...]
I think this is what you want to do:我认为这是你想要做的:
originalMSG = ['1','2','3','4','5','6','7','8','9','0','a','b','c','d','e','f','g','h','i','j','k','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z',' ']
codedMSG = ['v', 'u', '0', 's', '1', 'r', 'o', 'x', 'a', 'p', 'y', 'd', 'k', 'b', 'z', 'g', 'q', 'l', '5', ' ', 'w', 'c', 'm', '8', 'h', '4', 't', '3', '6', 'k', 'n', '7', 'j', '2', '9', 'e', 'i', 'f']
str2 = 'wello'
h=[]
for i in range(0, len(str2)):
h.append(codedMSG[originalMSG.index(str2[i])])
print(''.join(h))
Output: 2zmm4
输出:
2zmm4
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.