Printing mixtures in integers and strings
Enrique
ecastro at cicei.ulpgc.es
Fri Mar 30 09:48:12 EST 2001
Hi Neil,
Neil Benn wrote:
>
> Hello,
>
> I'm trying to use something like this:-
>
> def move(row): #Moves the dispenser to the row on the plate
> print "Moved to Row - " + row
You are trying to add (operator "+") a number and a string
>
> Where row is expected to be a number. However when I import this into
> teh interpreter I get an error message - stating that __add__ and __radd__
> have not been defined:-
>
> Traceback (innermost last):
> File "<console>", line 1, in ?
> File "c:\projects\scripting\TrialScripting\AcmeBRD.py", li
> TypeError: __add__ nor __radd__ defined for these operands
>
Which is the expected result since built-in strings and ints
do not add together
You can modify __add__ or __radd__ methods in these object
so they know how to add a number a string or the opposite.
It's easier to convert number to string : print "Moved to
Row - " + str(row),
or let's 'print' to do this conversion : print "Moved to Row
- ", row
(prints string "Moved to Row - " and then prints printable
form of var "row" which happens to be str(row))
Enrique
More information about the Python-list
mailing list