tags:

views:

62

answers:

3
A=s.append(s[i]+A+B)

A=s.append(s[i]+A+B) TypeError: unsupported operand type(s) for +: 'long' and 'str'

What does this error mean ? A and B are strings and s is a list

A: 

If A and B are strings, then s[i] must be a 'long'.

A: 

s[i] is likely a long. You can't add a long to a string.

Try:

A=s.append(str(s[i])+A+B)

Philippe Beaudoin
+4  A: 

s may be a list, but the element - s[i] - is not - it's a long, as indicated by the error.

In addition, append() operates on the list directly - it returns None, so you're actually setting A to be None - probably not what you wanted!

There are two things you can do to help avoid this type of error in the future.

  1. Don't use one-letter variable names. Use descriptive, one to three-word length names that describe what the variable has in it (and/or what it's supposed to be used for).

  2. When you do have a problem, try putting it in a try/except block where you put the error name after except and print out the offending variables:

try:
    s.append(s[i]+A+B)
except TypeError:
    print "Failed to add", s[i], ",", A, ",", "and", B
    raise

Don't forget the raise at the end there - that way you don't just ignore the issue and start getting really strange errors.

Daniel G