我想让代码打破游戏,其中用户提交符号/字母对字典来破解代码,然后我希望代码使用字典来用配对的字母替换符号的每个实例。python使用字典替换字符串列表中的字符
我有下面的代码位:
words = imported list of coded words where each letter is replaced by a symbol. from a text file so i can change later
clues = dictionary of symbol and letter pairs that can be added to, removed from
我曾尝试以下,但它失败:TypeError: list indices must be integers, not str
def converter(words,clues):
progression = words
for words in progression:#cycles through each coded word in the list
for key in clues: #for each symbol in the dictionary
progression[words] = progression[words].replace(key, clues[key]) #replaces
return progression
任何帮助,任何人都可以提供我将非常感激。
亚当
对于许多不同的事情,你正在重复使用相同的变量名,避免这种情况。另外 - 你提到字典,但只使用列表。 –