首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >python中ascii加密程序出错

python中ascii加密程序出错
EN

Stack Overflow用户
提问于 2017-11-12 13:43:40
回答 1查看 300关注 0票数 0

我有几个星期的编码,有一个任务要做,需要加密输入到ascii,或解密消息。我不能使用两种代码来加密和解密,这可以通过输入密码的否定版本(因此函数)来实现--我自己获得了加密,但很难将它作为一个包工作。程序需要从用户那里获得一个选项(加密或解密),然后使用从文本和密钥返回的值来转换主函数中的文本。我已经做了10个小时的研究和改变了很多元素,但似乎不断下降。任何建议都会很棒。运行时出现的错误,我将其放在代码下面。

代码语言:javascript
复制
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))

运行时:Python3.6.2 (v3.6.2:5fd33b5,7月8日,04:57:36) MSC v.1900 64位(AMD64)关于win32类型的“版权”、“信用”或"license()“以获得更多信息。

重新启动:EX6 test.py =您想要哪种模式?E用于加密,D用于解密。请输入一个句子来加密。hello输入passkey: 4 Traceback (最近一次调用):"C:\Users\ninja\AppData\Local\Programs\Python\Python36\ex6 test.py",第43行,打印( main (函数,消息,密码键)文件"C:\Users\ninja\AppData\Local\Programs\Python\Python36\ex6 test.py",第6行,main for x in message: TypeError:'function‘object不可迭代 ""“

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-11-12 14:27:12

在您的错误消息中,它表示一个'function' object is not iterable。这是因为您正在返回text,这是一个函数。

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

代码语言:javascript
复制
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))
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47249515

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档