Little problem with the "+" operator
Steve Purcell
stephen_purcell at yahoo.com
Mon Mar 5 10:01:56 EST 2001
Zamurai wrote:
> variableOne = 500
> myVariable = variableOne + "cool"
>
> But I get the following error: "unsupported operand types for +"
>
> Does someone know how to solve the problem?
You can't add variables of different types together in Python and expect them
to magically agree on a sensible result.
So, you have to explicitly say:
>>> variableOne = 500
>>> myVariable = str(variableOne) + "cool"
>>> myVariable
'500cool'
>>>
Or,
>>> variableOne = 500
>>> myVariable = variableOne + int("cool")
Traceback (innermost last):
File "<stdin>", line 1, in ?
ValueError: invalid literal for int(): cool
>>>
This is infinitely more sane than:
% perl -e 'print 500 + "cool"'
500
-Steve
--
Steve Purcell, Pythangelist
Get testing at http://pyunit.sourceforge.net/
Get servlets at http://pyserv.sourceforge.net/
"Even snakes are afraid of snakes." -- Steven Wright
More information about the Python-list
mailing list