【问题标题】:error with ascii encryption program in pythonpython中的ascii加密程序出错
【发布时间】:2018-04-25 06:15:34
【问题描述】:

我已经进行了几周的编码,并且有一项任务需要将文本输入加密为 ascii,或解密消息。我不能使用两个代码来加密和解密,这可以通过输入密钥的负版本(因此是函数)来完成,我让加密自己工作,但很难让它作为一个包工作。程序需要从用户那里得到一个选项(加密或解密,然后使用从文本和密钥返回的值来转换主函数中的文本。 我已经在这里工作了 10 个小时,进行了大量的研究和更改元素,但似乎一直在下降。提供的任何建议都会很棒。运行时的错误我已经放在代码下了。

def main(function, message, passkey):
    #takes value of mode and applies text. then uses key to encrypt or decrypt
    encrypt = ""
    for x in message:
    if x == " ":
        encrypt += " "
    else:
        encrypt += chr((((ord(x) - 65) + passkey % 26) + 65))


def mode():
    # determines either encryption or decryption. 
    func = input("Which mode would you like? E for encryption or D for decryption.\n").upper()
    if func[0] =='E': return 'E'
    elif func[0] == 'D': return 'D'
    else: print("Not a valid option. Please try again")

def text():
    #depending on value of mode, either input a sentance to encrypt or an ecrypted message to decode.
    if function == 'E':
        sentance = input("Please enter a sentance to encrypt.\n").upper()
        if all(x.isalpha or x.isspace() for x in sentance):
            return text
        else: sentance = input("Only uppercase alpha characters and spaces allowed. Try again.\n")
    else:
        return input("Enter coded message for decrypting:\n")


def key():
    #depending on value of mode, enter positve key to encrypt or same key in negative form to decrypt.
    if function == 'E':
        return abs(int(input("Enter passkey: ")))
    elif function == 'D':
        return -abs(int(input("Enter passkey: ")))

function = mode()
message = text()
passkey = key()

    print (message)
    print(main(function, message, passkey))

""" 运行时: Python 3.6.2(v3.6.2:5fd33b5,2017 年 7 月 8 日,04:57:36)[MSC v.1900 64 位(AMD64)] 在 win32 上 输入“copyright”、“credits”或“license()”了解更多信息。

重启:ex6 test.py = 你想要哪种模式? E 表示加密或 D 表示解密。 e 请输入要加密的句子。 你好世界 输入密码:4 回溯(最近一次通话最后): 文件“C:\Users\ninja\AppData\Local\Programs\Python\Python36\ex6 test.py”,第 43 行,在 打印(主要(功能,消息,密码)) 文件“C:\Users\ninja\AppData\Local\Programs\Python\Python36\ex6 test.py”,第 6 行,在 main 对于消息中的 x: TypeError: 'function' 对象不可迭代

"""

【问题讨论】:

    标签: python encryption


    【解决方案1】:

    在您的错误消息中显示'function' object is not iterable。那是因为你返回的是一个函数text

    要解决此问题,只需将第 28 行中的 return 语句更改为 return sentance。您还需要在main 中返回加密消息。

    def main(function, message, passkey):
        # takes value of mode and applies text. then uses key to encrypt or decrypt
        encrypt = ""
        for x in message:
            if x == " ":
                encrypt += " "
            else:
                encrypt += chr((((ord(x) - 65) + passkey % 26) + 65))
        return encrypt
    
    
    def mode():
        # determines either encryption or decryption.
        func = input("Which mode would you like? E for "
                     "encryption or D for decryption.\n").upper()
        if func[0] == 'E':
            return 'E'
        elif func[0] == 'D':
            return 'D'
        else:
            print("Not a valid option. Please try again")
    
    
    def text():
        # depending on value of mode, either input a sentance
        # to encrypt or an ecrypted message to decode.
        if function == 'E':
            sentance = input("Please enter a sentance to encrypt.\n").upper()
            if all(x.isalpha or x.isspace() for x in sentance):
                return sentance
            else:
                sentance = input("Only uppercase alpha characters "
                                 "and spaces allowed. Try again.\n")
        else:
            return input("Enter coded message for decrypting:\n")
    
    
    def key():
        # depending on value of mode, enter positve key to encrypt
        # or same key in negative form to decrypt.
        if function == 'E':
            return abs(int(input("Enter passkey: ")))
        elif function == 'D':
            return -abs(int(input("Enter passkey: ")))
    
    
    function = mode()
    message = text()
    passkey = key()
    
    print(message)
    print(main(function, message, passkey))
    

    【讨论】:

    • 非常感谢您的快速建议。更正返回值可以解决错误。它现在输出加密文本。有点。现在,如果我输入一个单词(即 hello),程序现在只返回一个字母(在本例中为 S)。当我在分配给函数之前制作该部分时,它会将每个字母作为加密版本输出。
    • 您确定复制正确吗?因为它对我有用(“HELLO”变成“LIPPS”)。
    • 奇怪..我把我的代码复制回来并运行它,它可以工作!非常感谢您的支持谢谢!
    猜你喜欢
    • 1970-01-01
    • 2017-08-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-07
    • 2017-12-07
    • 1970-01-01
    相关资源
    最近更新 更多