0

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

fizzybear
  • 1,197
  • 8
  • 22
Christian R
  • 293
  • 2
  • 10
  • 1
    Why would `text[a dictionary!]` work? The first thing that comes to mind is to iterate over the file's content letter by letter and do the substitution on each letter. There are other ways as well – ForceBru Dec 06 '19 at 20:07

1 Answers1

1

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()
fizzybear
  • 1,197
  • 8
  • 22
  • Now try to think what happens if the file is very very big. You are holding up a huge list in memory and then also adding a huge string. Try to think if that can be avoided – Tomerikoo Dec 06 '19 at 20:17
  • 1
    I agree that this would be a problem in certain cases. However, it's quite an orthogonal concern to the OP's question. It seems inappropriate to increase the complexity of the answer to such an extent when the OP is clearly a novice to the language. – fizzybear Dec 06 '19 at 20:20
  • Making that change it gives me a TypeError: string indices must be integers – Christian R Dec 06 '19 at 20:59
  • Which string? `subs_text`? – fizzybear Dec 06 '19 at 21:03
  • line 8: subs_text.append(letter[letters]) – Christian R Dec 06 '19 at 21:07
  • It's the other way around. `letters[letter]` – fizzybear Dec 06 '19 at 21:08
  • If I swap it that way now I am getting: AttributeError: 'str' object has no attribute 'append' – Christian R Dec 06 '19 at 21:08
  • 1
    subs_text is a list. You might be using the overwritten one after merging all the characters with `"".join`. – fizzybear Dec 06 '19 at 21:10
  • Fizzybear, thank you for the help. The problem ended up being that the subs_text line was indented in the for loop and that there was a question mark in the text file which was giving a key error. Got it figure out, thank you for the help. What would be the best way to go about now decrypting the encrypted file? – Christian R Dec 07 '19 at 20:56
  • You need to invert your `letters` dict and use this new dict to perform substitution on the ciphertext. https://stackoverflow.com/questions/483666/reverse-invert-a-dictionary-mapping – fizzybear Dec 07 '19 at 22:44