Skip to content
Advertisement

Is there a way to transfer the contents of a for statement to a function?

def encrypt():
  while True:
    try:
        userinp = input("Please enter the name of a file: ")
        file = open(f"{userinp}.txt", "r")
        break  
    except:
      print("That File Does Not Exist!")
  second = open("encoded.txt", "w")
  for line in file:
    swapped(line)
    break
  #second.write()  
 
def swapped(line):
  newword = ""
  arranged = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"]
  random = ["q","w","e","r","t","y","u","i","o","p","a","s","d","f","g","h","j","k","l","z","x","c","v","b","n","m"]
  print(line)

So i’m currently trying to make a program that encrypts, then decrypts a file. For the encryption process i’m trying to move a line of the given file to another function, that will replace a letter at one index from the arranged list corresponding to the words in the file, with a letter at the same index, but isntead in the random list.

I’ve so far attempted to call the swapped function under the for loop that I created at the end of my while statement, and although successful in transferring everything within the file, when using a for loop for any of the given lists in the second function, the whole list gets printed out 9 times, which is the number of lines in the given file. Up above I tried to make it so that the swapped function is called under the for loop, but then breaks, which only transports one line of the file to the second function, but makes the lists print out the letters normally.

Right now I just need help with transferring the contents of my file in a way that each line is individually carried to swapped function, but also in a way that the lists work properly allowing me to swap values.

I’m still fairly new to python, so help would be appreciated.

Advertisement

Answer

As is, your swapped function just prints the input as you haven’t actually used the arranged and random lists. I’d suggest changing the lists to a dictionary :

def encrypt():
  while True:
    try:
        userinp = input("Please enter the name of a file: ")
        file = open(f"{userinp}.txt", "r")
        break  
    except:
      print("That File Does Not Exist!")
  second = open("encoded.txt", "w")
  encrypted_lines = []
  #Creates a list of encrypted lines
  for line in file:
     encrypted_line = swapped(line)
     encrypted_lines.append(encrypted_line)
  return encrypted_lines
  #TODO save to encrypted file instead of returning
 
def swapped(line):
    new_line = ""
    encrypt_dict =  {'a': 'q', 'b': 'w', 'c': 'e', 'd': 'r', 'e': 't', 'f': 'y', 'g': 'u', 'h': 'i',
    'i': 'o', 'j': 'p', 'k': 'a', 'l': 's', 'm': 'd', 'n': 'f', 'o': 'g', 'p': 'h',
        'q': 'j', 'r': 'k', 's': 'l', 't': 'z', 'u': 'x', 'v': 'c', 'w': 'v', 'x': 'b',
    'y': 'n','z': 'm'}
    for i in line:
        if i in list(encrypt_dict.keys()):
            new_line = new_line + encrypt_dict[i]
    return new_line
print(encrypt())
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement