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

Viewed 43

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

2 Answers

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]

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
Related