尝试将字符串加密为ASCII

时间:2014-11-13 21:30:12

标签: python encryption

这是我不确定哪里出错:

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')

1 个答案:

答案 0 :(得分:1)

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

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

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

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