【问题标题】:Encryption Program: TypeError: object of type 'builtin_function_or_method' has no len()加密程序:TypeError:“builtin_function_or_method”类型的对象没有len()
【发布时间】:2015-08-22 16:32:02
【问题描述】:

我在 Python 3 中创建了一个关键字加密程序,但遇到了一个我不知道如何解决的错误。错误是: TypeError: 'builtin_function_or_method' 类型的对象没有 len() 它发生在我的代码的第 18 行。

ans = False
print(""" *****Hello. Welcome to the Vignère Cipher Encryption Program*****
    ***This program uses a keyword that is repeated until it
    matches the same lenght of the message and then adds its
    numerical value to the numerical value of the message and
    outputs the encrypted message in alpha. 
    Please press:
    E to Encrypt
    D to Decrypt
    or  double tap enter to quit.
    """)

ans=input("What would you like to do now???")
if ans == "E":
    plaintext = str(input("Please enter a message to be encrypted: ")).upper
    keyword = str(input("Please enter a keyword to be used to encrypt a message (alpha only): ")).upper
    ciphered = " "
    for i in range (len(plaintext)):
        char = plaintext[i]
        alphakeywordvalue = ord(keyword[i%len(keyword)]) - ord("A")+1
        if char.isupper():
            if cipher == "E" :
                value = ord(char) + alphakeywordvalue 
                if value > ord("Z"): 
                    value -= 26
                    print ("Your encrypted text is:", ciphered)

elif ans == "D":
   plaintext = str(input("Please enter a message to be dencrypted: ")).upper
   keyword = str(input("Please enter a keyword to be used to dencrypt a message (alpha only(make sure that it is the same keyword used to encrypt the message)): ")).upper
   ciphered = " "
   for i in range (len(plaintext)):
        char = plaintext[i]
        alphakeywordvalue = ord(keyword[i%len(keyword)]) - ord("A")+1
        if char.isupper():
            if cipher == "D" :
                value = ord(char) - alphakeywordvalue
                if value <ord("A"):
                    value += 26
                ciphered += chr(value)

【问题讨论】:

  • 你应该使用 raw_input 而不是输入法

标签: python python-3.x encryption


【解决方案1】:

您忘记了实际调用方法的上部括号:

 str(input("Please enter a message to be dencrypted: ")).upper <- should be upper()

在下一行:

 is the same keyword used to encrypt the message)): ")).upper <- should be upper()

在python3中,输入已经是一个字符串,所以在它上面调用str是多余的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-02-15
    • 1970-01-01
    • 2015-08-21
    • 2019-05-23
    • 2013-04-18
    • 2015-01-21
    • 2021-12-19
    相关资源
    最近更新 更多