I'm in the process of making an encrypting/decrypting code and here's what I have so far...
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:
line = line.strip()
swap = swapped(line)
print(swap)
change_char = change_character(swap)
reversed = reverse_word(change_char)
second.write(reversed)
file.close()
second.close()
def swapped(line):
line = line.strip()
new = ''
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"]
for character in line:
i = arranged.index(character)
if arranged[i] == random[i]:
new = new random[i]
def change_character(swap):
empty = []
secondary = ""
num = ord(swap)
empty.append(num)
for i in range(len(empty)):
if num == 32:
num = 9888
else:
num = num 80
secondary = secondary chr(num)
return secondary
def reverse_word(change_char):
data_1 = change_char[::-1]
return data_1
def decrypt():
file = open(f"encoded.txt", "r")
decode = open("decrypted.txt", "w")
for line in file:
un = unswap(line)
change = unchange_char(un)
normal = unreverse(change)
decode.write(normal)
file.close()
decode.close()
I'm essentially trying to modify a text file in 3 ways, paste it to another file, then use that file to decrpyt the text file, and place it inside of another file.
The only real problem I'm having with doing so, is making one of my changing function work, which is the very first one after the encrypt function called "swapped". What i'm trying to do is search the text file for words that fit in the arranged list, then swap those letters with letters from the random list at the same index so that the text file changes correspondingly to what's changed. I've tried many different things, and with what i'm trying right now, I get the error...
"" Is Not In The List,
even when just printing out arranged[i]. I've tried basically everything I could think of, and have had no luck. I would like for the function to stay using the two lists that i've created if possible. Thanks!
CodePudding user response:
The reason you are getting that error is because the space character is not in the list, so you have two options:
- You can either add a space into both lists.
- You can check to see if the character you are currently on is a space.
That will solve that issue, but I am not sure if the rest of the logic in that function is correct, at this point it will only swap characters if they are the same characters in the exact same slots, which does not happen, meaning you will always get an empty string.
Here is what I would do, with out changing you function that much:
def swapped(line):
line = line.strip()
new = ''
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 = ["a","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"]
for character in line:
if character == ' ':
new = ' '
else:
i = arranged.index(character)
new = new random[i]
print(new)
swapped("lol this is just a test")