-2
file_ascii = [(ord(c)) for c in contents]
f_file = []
for x in range (0, len(file_ascii)): 
    if file_ascii[x] != 32:
        file_ascii2 = (file_ascii[x])
        file_ascii2 = (offset) + (file_ascii2)
        if file_ascii2 > 126:
            file_ascii2 = (file_ascii2) - 94
    print (file_ascii2)
    file_ascii2 = [(chr(i)) for i in file_ascii2]
    f_file.append(file_ascii2)

everything in the list 'contents' is supposed to be turned into its equivalent ascii code. however when 'file_ascii2' is turned back into normal letters, some of it is already a normal letter, even thought it has been added and subtracted from already. also when the code is run with

file_ascii2 = [(chr(i)) for i in file_ascii2]

as a comment, 'file_ascii2' is proven to be all integers

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Omer Hyman
  • 7
  • 1
  • 2

1 Answers1

1

You never changed the elements in the file_ascii list. You only changed the variable file_ascii2. You need to assign back to the list for the changes to be reflected there:

if file_ascii[x] != 32:
    file_ascii2 = (file_ascii[x])
    file_ascii2 = (offset) + (file_ascii2)
    if file_ascii2 > 126:
        file_ascii2 = (file_ascii2) - 94
    file_ascii[x] = file_ascii2

or simply directly append the chr() result for that number back to the f_file list.

You should really use better names for your variables; file_ascii is a list of numbers representing characters, file_ascii2 is one such number, so your list comprehension trying to use a for loop over that number will fail.

Cleaning up your code a little, without changing the technique, results in:

file_ascii = [ord(c) for c in contents]
f_file = []
for index in range(0, len(file_ascii)): 
    codepoint = file_ascii[index]
    if codepoint != 32:
        codepoint += offset
        if codepoint > 126:
            codepoint -= 94
    f_file.append(chr(codepoint))

It'd be easier to just loop over contents and convert each character to an integer in the loop:

f_file = []
for character in contents:
    codepoint = ord(character)
    if codepoint != 32:
        codepoint += offset
        if codepoint > 126:
            codepoint -= 94
    f_file.append(chr(codepoint))
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343