List Python-TypeError:列表索引必须是整数,而不是str
我正在写一个非常非常简单的加密脚本 当我运行它时,发生了一个类型错误: TypeError:列表索引必须是整数,而不是str' 我查看了一些有关Stackoverflow的问题,但没有任何帮助 我想这是一个非常简单的观点,我在这里错过了List Python-TypeError:列表索引必须是整数,而不是str,list,typeerror,List,Typeerror,我正在写一个非常非常简单的加密脚本 当我运行它时,发生了一个类型错误: TypeError:列表索引必须是整数,而不是str' 我查看了一些有关Stackoverflow的问题,但没有任何帮助 我想这是一个非常简单的观点,我在这里错过了 import os os.system("clear") content = input("content:" + " ") filename = input("filename:" + " ") content = list(content) file
import os
os.system("clear")
content = input("content:" + " ")
filename = input("filename:" + " ")
content = list(content)
file = open(filename, "w")
counter = 0
os.system("clear")
input("Press enter to encrypt...")
for counter in content:
if content[counter] in "a":
content[counter] = "z"
if content[counter] in "b":
content[counter] = "y"
if content[counter] in "c":
content[counter] = "x"
if content[counter] in "d":
content[counter] = "w"
if content[counter] in "e":
content[counter] = "v"
if content[counter] in "f":
content[counter] = "u"
if content[counter] in "g":
content[counter] = "t"
if content[counter] in "h":
content[counter] = "s"
if content[counter] in "i":
content[counter] = "r"
if content[counter] in "j":
content[counter] = "q"
if content[counter] in "k":
content[counter] = "p"
if content[counter] in "l":
content[counter] = "o"
if content[counter] in "m":
content[counter] = "n"
if content[counter] in "n":
content[counter] = "m"
if content[counter] in "o":
content[counter] = "l"
if content[counter] in "p":
content[counter] = "k"
if content[counter] in "q":
content[counter] = "j"
if content[counter] in "r":
content[counter] = "i"
if content[counter] in "s":
content[counter] = "h"
if content[counter] in "t":
content[counter] = "g"
if content[counter] in "u":
content[counter] = "f"
if content[counter] in "v":
content[counter] = "e"
if content[counter] in "w":
content[counter] = "d"
if content[counter] in "x":
content[counter] = "c"
if content[counter] in "y":
content[counter] = "b"
if content[counter] in "z":
content[counter] = "a"
content = "".join(content)
file.write(content)
file.close()
os.system("clear")
print("Successfully encrypted!")
print("Use 'decrypt.py' to decrypt.")
当您开始过多地按Ctrl+V时,请后退一步。。在任何情况下,计数器都是表示正在迭代的字符的字符串,因此内容[counter]因异常消息中给出的原因而无效。换句话说,运行以下命令:for content:printcounter中的计数器,而不是有问题的循环,并添加适当的格式;你看到了什么结果?为什么?使用该观察返回原始问题。如果要迭代序列的索引,请使用。但这并不是真正想要的提示:在Python中字符串是不可变的,这样的单个字符是不能更改的。相反,创建一个不同的变量,比如encryped_content=并在每个循环中向其追加适当的输出。在循环内部,您应该只需要访问计数器,并将其称为其他名称,例如字母,以避免混淆和此加密的内容。