首页 > 解决方案 > 为什么我在解密程序中收到有关字符串的错误?

问题描述

我正在尝试制作某种解密器。该程序要求您输入一个短语,它会解密它。例如,如果我输入“hi”,它应该显示为“ef”。但是,我得到的只是一个错误,TypeError: unsupported operand type(s) for -: 'str' and 'str'

我不知道该怎么做,我试过摆弄代码但没有好的结果。

def plainText(string):
    cipher = ''
    for i in string:
        if i == ' ':
            cipher = cipher - i
        elif i.isupper():
            cipher = cipher + chr((ord(i) - 3 - 65) % 26 + 65)
        else:
            cipher = cipher + chr((ord(i) - 3 - 97) % 26 + 97)
    return cipher
message = input("What's your message?")
print("Your decrypted message is: ", plainText(message))
print("Your original message was: ", message)```

Again, the error was

TypeError: unsupported operand type(s) for -: 'str' and 'str' at line 7. 

Thank you in advance!

标签: pythonpython-3.xencryption

解决方案


推荐阅读