Thomas Torrington
Thomas Torrington

Reputation: 11

Encryption Program: TypeError: object of type 'builtin_function_or_method' has no len()

I have created a keyword encryption program in Python 3 but I have run into an error I don't know how to solve. The error is: TypeError: object of type 'builtin_function_or_method' has no len() It occurs on line 18 of my code.

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)

Upvotes: 0

Views: 478

Answers (3)

Hrutik Gurav
Hrutik Gurav

Reputation: 1

string = input("Enter a string:")
   
#converting to lower case
string = string.lower
   
#calculating total length of String
length = len(string)
   
#counter to count vowels
counter=0
   
for k in string:
    if k=='a' or k=='e' or k=='i' or k=='o' or k=='u':
        counter=counter+1
   
#print("Number of vowels is:",counter)
print("Number of consonants is:", length-counter)

I got a similar error in the above piece of code. As per checking, I found that on line 4 I forget to give "()" after the lower function check same. Check for such typos or syntax errors in your code.

Upvotes: 0

abdeali004
abdeali004

Reputation: 473

Error- TypeError: object of type 'builtin_function_or_method' has no len() comes when you call length function (len()) on the address of another function.

Such as:

print(sqrt(4)) ----> Gives 2

But

print(sqrt) -----> Gives <built-in function sqrt> (Address)

So, use value.upper() (With brackets '()'). Because value.upper will return ---> <built-in method upper of str object at 0x000001B855732170> on which len() gives error.

Upvotes: 0

Padraic Cunningham
Padraic Cunningham

Reputation: 180391

You forgot the parens from upper to actually call the method:

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

And on the next line:

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

In python3, input is already a string so calling str on it is redundant.

Upvotes: 3

Related Questions