I have made a program in Python 3, which tests whether a number is a palindrome. Obviously it is not done, but when I try to run it:
#!/usr/bin/env python
def testforpalin():
i = 101
lop = list(str(i))
print(lop)
len(lop)
if lop[0] == lop[len-1]:
print("hi")
testforpalin()
TypeError: unsupported operand type(s) for -: 'builtin_function_or_method' and 'int'
I get that error. How do I fix this?
in the line
you have
len-1wherelenis the function that gives you the length of the string (hence the error – you are trying to do subtraction where one of the values islenwhich is a “builtin_function_or_method”). you probably meanlop[len(lop)-1](which would work), but it would be simpler to do:because
[-1]gives you the last element in a string or array (and[-2]gives you next-to-last, etc).