正在尝试将字符串加密为ASCII

2024-06-28 11:23:52 发布

您现在位置:Python中文网/ 问答频道 /正文

这就是我不确定的地方:

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

Tags: keyinfromimportmessageforinputhere
1条回答
网友
1楼 · 发布于 2024-06-28 11:23:52
Key = int(input("enter a key from 1-100:"))

input是字符串,需要转换为整数。你知道吗

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

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

相关问题 更多 >