How can i get rid of this error?'builtin_function_or_method' object is not subscriptable

Question:

My code is below:

def count(lst):
    even=0
    odd=0
    for i in lst:
        if i%2==0:
            even+=1
        
        else:
            odd+=1
    return even,odd
        
lst=[11,13,16,17,19,20]
even,odd=count(lst)
print("Even : {} and Odd :{}",format[even,odd])

When I run,i get following error pointing to last line:
‘builtin_function_or_method’ object is not subscriptable

I am trying my best to follow this tutorial but still i got error

Asked By: engr

||

Answers:

It should be:

print(f"Even: {even} and Odd: {odd}")

You have to use a f-string which will help convert variables into your strings. Tip: put your variables in the curly brackets.

Or if you want to use the .format:

print("Even: {} and Odd: {}".format(even, odd))

The .format function uses the following format:

str.format()

Your errors:

  • You used square brackets (which are used for lists or indexes) instead of normal brackets
  • You used , instead of . to denote the function
Answered By: DialFrost

I think you mean to write

print("Even : {} and Odd :{}".format(even,odd))

As the error clearly says, you try to reach an index of the built-in function (format in this case) by writing

format[even, odd]
Answered By: Nuri Taş
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.