python 3.x - ord function doesn't work -
file_ascii = [(ord(c)) c in contents] f_file = [] 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)) in file_ascii2] f_file.append(file_ascii2) everything in list 'contents' supposed turned equivalent ascii code. when 'file_ascii2' turned normal letters, of normal letter, thought has been added , subtracted already. when code run with
file_ascii2 = [(chr(i)) in file_ascii2] as comment, 'file_ascii2' proven integers
you never changed elements in file_ascii list. changed variable file_ascii2. need assign list changes 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 directly append chr() result number f_file list.
you should use better names variables; file_ascii list of numbers representing characters, file_ascii2 one such number, list comprehension trying use for loop on number fail.
cleaning code little, without changing technique, results in:
file_ascii = [ord(c) c in contents] f_file = [] 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 easier loop on contents , convert each character integer in loop:
f_file = [] character in contents: codepoint = ord(character) if codepoint != 32: codepoint += offset if codepoint > 126: codepoint -= 94 f_file.append(chr(codepoint))
Comments
Post a Comment