Decoding encrypted text

I’ve written some code but overall am stuck on how to do this. I have word out how much each letter is moved in the alphabet to code my txt file (I have to move each letter by 7, i.e an a in the file would be t when translated, b would be u, m would be f ect).
I think I need to make each letter in each line a string so that I can ‘move’ each of the letters back 7 so it moves to normal English. However, I’m not sure what code to use to get this - I could use .append but I would have to change every single letter individually.

Would someone mind helping in a way that incorporates the use the dictionaries?

Hi @Icantcode , welcome!

  1. You can code, I promise :laughing: it just takes practice!
  2. There’s more than one way to do this and you don’t necessarily have to use a dictionary. Is this an assignment/coursework that requires you to use a dictionary?

You may find the built in functions chr and ord helpful!

1 Like

Have a look at the maketrans and translate methods of str.

1 Like

Haha thank you! I am trying - it just takes me a lot longer to get anywhere and a lot of support!

It is coursework and we have to use the lessons we have been taught in lessons I think rather than just importing packages to help us.
I will have a look into those thank you!

Hi, So I have worked on it a bit more:


which is fine but with my text file I have opened when I press to run it says
FileNotFoundError: [Errno 2] No such file or directory: ‘hi.txt’
however, this file is in my computer files but not in my working directory - I’ve had a search but cannot work out how to get something in my working directory.
Also, do you think this code will work to change the ordering of my letters?

If you do know which directory is*the working directory, and really just want to put the file there, then you do that the same way that you would put any file anywhere. This isn’t a programming question; it’s a “how do I use my computer” question. For example, in Windows you can drag and drop it into the appropriate Explorer window.

If you want to change what the working directory is, please read:

if you want to find out what the current working directory is, please read:

If you want to locate the file relative to the script’s location (instead of caring what the current working directory is), you do this by changing the current working directory to the script’s location. That starts with finding out what that path is. Please read:

To get the whole file as one string, use the read method instead of readlines.

Other than that, it looks like it’ll work, at least with the lowercase letters. It won’t do anything to the uppercase letters, though, so if your input file also has uppercase, you’ll want to account for that. You can just add uppercase letters where you already have the strings with all the lowercase letters.

1 Like