Skip to main content
 首页 » 编程设计

python之尝试将字符串加密为 ASCII

2025年05月04日18zhoujg

这是我不确定哪里出错了:

import sys 
message = input("enter message here:") 
Key = input("enter a key from 1-100:") 
 
for Letter in message: 
        Char = ord(Letter) 
        if (Char + Key) < 32: 
            encryptedChar = ((Char - Key) + 127) - 32 
        else: 
            encryptedChar = (Char - Key) 
            sys.stdout.write(chr(encryptedChar)) 
 
            print(encryptedChar,end=" ") 

我目前收到错误:

TypeError: unsupported operand type(s) for +: 'int' and 'str') 

请您参考如下方法:

Key = int(input("enter a key from 1-100:")) 

input是一个字符串,需要转换成一个整数。

转换为 int 后,您的代码运行良好:

enter message here:foobar 
enter a key from 1-100:10 
\92e101e101X88W87h104