【发布时间】: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