So what I need to do is read in the file \"clues.txt\" and store it into a dictionary... So far I have got it to store into a list but am struggling to do it with a dictionary..
Try this it will work according to the pattern of clues file you posted:
d = {}
with open("file.txt") as f:
for line in f:
(key, val) = line[1], line[0]
d[key] = val
Try this for reading dictionary key value pairs and replacing symbols:
for word in range(len(words)):
for key, value in d.items():
words[word] = words[word].replace(key, value)
EDIT:
d = {}
def read_clues(clues):
global d
with open("hey.txt") as f:
for line in f:
(key, val) = line[1], line[0]
d[key] = val
def replace_symbols(clues, words):
global d
for word in range(len(words)):
for key, value in d.items():
words[word] = words[word].replace(key, value)
Just put this code in a .py
file and run it, it will work. What you are doing is you are trying to call a local variable d
outside of its scope that is why you were getting the error now I have made that d
variable global.
#REPLACES LETTERS
print("======== The clues have been replaced ===========")
replace_symbols(clues, words)
for key, value in d.items():
print key, value #This will print the symbols and letters