Write a program that uses a dictionary to assign codes to ea

Write a program that uses a dictionary to assign “codes” to each letter of the alphabet. For example:

codes = {\'A\': \')\', \'a\': \'0\', \'B\': \'(\', \'b\': \'9\', etc}

Using this code, any occurrence of letter ‘A’ would be replaced with a ‘)’, any ‘a’ would be replaced with a ‘0’, and so forth. I have provided you a text file, codes.txt. This file contains the codes for all characters. The first few lines of the file are:

A ), a 0, B (, b 9, C *, c 8, D &, d 7, E ^, e 6

Using this file, create a dictionary, with the data in first column as the keys and the corresponding second column element as the value. Once you have created the dictionary, you can open a file and use the dictionary to write an encrypted version of a file, changing every occurrence of a key, with the corresponding value.

‘aBc’ ‘0(8’

If you look deeper into the file, you’ll see that this ‘code’ works in reverse as well. For example,

‘A’ is the key to value‘)’ and

‘)’ is the key to value ‘A’.

This property will allow us to use the same code to encrypt and then decrypt our files. Write a program that prompts the user to either encrypt or decrypt a file. Based on the user’s response, perform the requested action.

A sample run:

Welcome to my encryption program.

You can choose to encrypt a file or decrypt an encrypted file.

What would you like to do:

1 - Encrypt a file

2 - Decrypt a file Your choice? 1

Enter the name of the input file: examplefile.txt

Enter the name of the output file: output.txt

Writing encrypted data to output.txt examplefile.txt

this is a line

it has three lines

this is the last line output.txt

q32r 2r 0 y2w6

2q 30r q3s66 y2w6r q32r 2r q36 y

0rq y2w6

HINTS: When you are converting characters, do not changes spaces, leave them as spaces. Your program should not include redundant code. If you’re doing the same task multiple times in your program, put it into a function and call the function!

Solution


# This program will replace any instances of a string with the specified coded character

# Ask the user for thier input
def getChoice():

   print(\'Welcome to my encryption program.\')
   print(\'You can choose to encrypt a file or decrypt a file.\')
   print(\'Would you like to:\')
   print(\'1. Encrypt a file.\')
   print(\'2. Decrypt a file.\')
   choice = input(\'Your choice: \')

   return choice

# start the main function
def main():

   # Start Error Handling
   try:

       # Start the code dictionary
       encCode = {}
       decCode = {}

       # Open the codes files
       codeKey = open(\'codes.txt\', \'r\')

       # Populate the codes dictionary
       for indKey in codeKey:

           encCode[indKey[0]] = indKey[2]
           decCode[indKey[2]] = indKey[0]

       # Get the users choice
       choice = getChoice()

       # Encrypt a file
       if choice == \'1\':

           # Get the input and output file names from the user
           inFile = input(\'Enter the name of the input file. \')
           outFile = input(\'Enter the name of the output file. \')
           print(\'Writing encrypted data to\', outFile)

           # Open the files
           inputFile = open(inFile, \'r\')
           outputFile = open(outFile, \'w\')

           # Convert the input file
           # Index through the lines
           for line in inputFile:

               # Start the encrypted list for each line as you index into each line
               encryptList = []

               # index through each character in the string
               for i in line:
                  
                   # See if we can encrypt the charachter
                   if i in encCode:

                       # Encrypt the letter
                       encryptList.append(encCode[i])

                   # Just pass the character through
                   else:
                       encryptList.append(i)

               # Join the line which is a list into one string & Write the line to the output file
               oneString = \'\'.join(encryptList)
               outputFile.write(oneString)

               # Close all the files
               inputFile.close()
               outputFile.close()
              
       # Decrypt a file
       if choice == \'2\':

           # Get the input file name from the user
           inFile = input(\'Enter the name of the input file. \')
           print(\'The decrypted contents of the file are: \')

           # Open the input file
           inputFile = open(inFile, \'r\')

           # Index through each line
           for line in inputFile:

               # Start the decrypted list
               decryptList = []

               # Index through each character
               for i in line:

                   # Can the character be encrypted
                   if i in decCode:

                       # Decrypt the letter
                       decryptList.append(decCode[i])

                   else:

                       # Just pass the character through
                       decryptList.append(i)

               oneString = \'\'.join(decyptList)
               print(oneString)

   # Error Handling
   except FileNotFoundError as err:
       print(\'Please check you file name.\')

   except Exception as err:
       print(\'An unknown error occured.\')
       print(err)

# run the main function
main()

examplefile.txt

this is a line
it has three lines
this is the last line

output.txt

q32r 2r 0 y2w6
2q 30r q3s66 y2w6r
q32r 2r q36 y0rq y2w6

Write a program that uses a dictionary to assign “codes” to each letter of the alphabet. For example: codes = {\'A\': \')\', \'a\': \'0\', \'B\': \'(\', \'b\':
Write a program that uses a dictionary to assign “codes” to each letter of the alphabet. For example: codes = {\'A\': \')\', \'a\': \'0\', \'B\': \'(\', \'b\':
Write a program that uses a dictionary to assign “codes” to each letter of the alphabet. For example: codes = {\'A\': \')\', \'a\': \'0\', \'B\': \'(\', \'b\':
Write a program that uses a dictionary to assign “codes” to each letter of the alphabet. For example: codes = {\'A\': \')\', \'a\': \'0\', \'B\': \'(\', \'b\':

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site