Reputation: 313
This is an assignment for school. I am using the following code:
def main():
letters = create_letters()
infile = open('note.txt', 'r')
text = infile.readlines()
infile.close()
outfile = open('notetranslate.txt', 'w')
outfile.write(text[letters])
outfile.close()
def create_letters():
return {'A':'`', 'a':'~', "B":'1', 'b':'!', 'C':'2', 'c':'@', 'D':'3', 'd': '#', 'E':'4',
'e':'$', 'F':'5', 'f':'%', 'G':'6', 'g':'^', 'H':'7', 'h':'&', 'I':'8', 'i':'*', 'J':'9', 'j':'(',
'K':'0', 'k':')', 'L':'-', 'l':'_', 'M':'=', 'm':'+', 'N':'[', 'n':'{', 'O':']', 'o':'}',
'P':'|', 'p':'/', 'Q':';', 'q':':', 'R':',', 'r':'<', 'S':'.','s':'>','T':'?', 't':'"',
'U':'`', 'u':'~', 'V':'1', 'v':'!', 'W':'2', 'w':'@', 'X':'3', 'x':'#', 'Y':'4','y':'$',
'Z':'5', "z":'%'}
main()
Upon running this code I get an error: list indices must be integers or slices, not dict.
My task is to write a program that will read the contents of the text file, then use the dictionary of codes to write an encrypted version of the files contents to a separate text file. Each character in the second text file should contain the encrypted version of text.
Write a second program (or add a menu of options for the user to your current program) that opens an encrypted version and displays the decrypted text back on the screen for the user to read
Upvotes: 0
Views: 80
Reputation: 1227
The objective is to substitute character by character the string with the dict in letters
.
infile = open('note.txt', 'r')
text = infile.read() # read not readlines
infile.close()
subs_text = []
for letter in text:
subs_text.append(letters[letter]) # lookup the character to be substituted with
subs_text = "".join(subs_text)
outfile = open('notetranslate.txt', 'w+') # use w+ to create file if it does not exist
outfile.write(subs_text)
outfile.close()
Upvotes: 1