我想讓代碼打破遊戲,其中用戶提交符號/字母對字典來破解代碼,然後我希望代碼使用字典來用配對的字母替換符號的每個實例。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
任何幫助,任何人都可以提供我將非常感激。
亞當
對於許多不同的事情,你正在重複使用相同的變量名,避免這種情況。另外 - 你提到字典,但只使用列表。 –