-2
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

1

3 Answers 3

4

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

0

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

Try:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.