我在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)
答案 0 :(得分:3)
你忘记了上层的parens来实际调用方法:
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是多余的。