Python 如何识别与条件对应的列表元素并应用函数

Python 如何识别与条件对应的列表元素并应用函数,python,list,encryption,ascii,Python,List,Encryption,Ascii,所以我需要一个加密文本文件的程序: for x in range(1): random_generated=(random.sample(range(33,126), 8)) random_values=(random_generated[0:]) print random_values ######using the random function to generate numbers from 33 to 126, 8 times######

所以我需要一个加密文本文件的程序:

for x in range(1):
    random_generated=(random.sample(range(33,126), 8))
    random_values=(random_generated[0:])
    print random_values
    ######using the random function to generate numbers from 33 to 126, 8 times######

    
    ascii="".join([chr(i) for i in random_values])   ###to convert numbers to ASCII characters### 
    print ascii     ###displaying to user###         ###source: stackoverflow.com################

    offset_factor=(((sum(random_generated[0:]))//8)-32)   ###to calculate the offset factor based on the generated###
    print offset_factor

    opentxt= open(filename, 'r').read()   ###source: ubuntuforums.org###
    print opentxt    ###opentxt being the contents of sample.txt###

    
    converted=[ord(c) for c in opentxt]   ###conversion of text into a list of ascii###
    print converted
    

    
    offset_ADD=[s + offset_factor for s in converted]
我已经到了一个地步,我已经创建了一个基于文本文件的ascii代码列表,并在列表的每个ascii元素中添加了一个随机生成的数字。任务要求:

开发将消息中的每个字符转换为ASCII码并使用以下规则对其进行加密的程序部分:

a。如果字符是空格,则不要加密字符
B否则:
我将字符转换为ASCII码(例如B=66)
二,。将ASCII码添加到任务4中计算的偏移因子中。如果结果大于126,则减去94,使结果为有效的ASCII码
iii.将结果转换为等效的ASCII字符

我已经完成了bi和bii的一半,但bii和biii的另一半我有问题

如果问题不够清楚,请务必要求解释课程的部分内容

这是我执行程序的时候:

1. Encrypt Message 2. Decrypt message 3. Quit
Please enter your selection 1
Option 1 selected
enter filename you want to encryptsample.txt
[35, 121, 52, 49, 63, 56, 58, 47]
#y41?8:/
28
Somewhere in la Mancha, in a place whose name I do not care to remember, a  gentleman lived not long ago, one of those who has a lance and ancient shield on a shelf and keeps a skinny nag and a greyhound for racing.
[83, 111, 109, 101, 119, 104, 101, 114, 101, 32, 105, 110, 32, 108, 97, 32, 77, 97, 110, 99, 104, 97, 44, 32, 105, 110, 32, 97, 32, 112, 108, 97, 99, 101, 32, 119, 104, 111, 115, 101, 32, 110, 97, 109, 101, 32, 73, 32, 100, 111, 32, 110, 111, 116, 32, 99, 97, 114, 101, 32, 116, 111, 32, 114, 101, 109, 101, 109, 98, 101, 114, 44, 32, 97, 32, 103, 101, 110, 116, 108, 101, 109, 97, 110, 32, 108, 105, 118, 101, 100, 32, 110, 111, 116, 32, 108, 111, 110, 103, 32, 97, 103, 111, 44, 32, 111, 110, 101, 32, 111, 102, 32, 116, 104, 111, 115, 101, 32, 119, 104, 111, 32, 104, 97, 115, 32, 97, 32, 108, 97, 110, 99, 101, 32, 97, 110, 100, 32, 97, 110, 99, 105, 101, 110, 116, 32, 115, 104, 105, 101, 108, 100, 32, 111, 110, 32, 97, 32, 115, 104, 101, 108, 102, 32, 97, 110, 100, 32, 107, 101, 101, 112, 115, 32, 97, 32, 115, 107, 105, 110, 110, 121, 32, 110, 97, 103, 32, 97, 110, 100, 32, 97, 32, 103, 114, 101, 121, 104, 111, 117, 110, 100, 32, 102, 111, 114, 32, 114, 97, 99, 105, 110, 103, 46]

                

那么你有什么问题?您是否有可以向我们展示的堆栈跟踪?您可能需要阅读(特别注意帮助他人重现问题),并相应地重写/修改您的问题。