[Tutor] calculation in string operations

David ldl08 at gmx.net
Thu Aug 4 01:16:39 CEST 2011


Hello everybody,

I got an error message that surprises me. I know how to solve the
problem (couple of parentheses will do), but I would expect Python to
run as the code is presented below.
After all, my_weight is a number that can be multiplied by a float.
I would expect Python to evaluate the expression after the (final)
operator and then having it inserted into the string.

So I guess my question is this: what is the path that Python follows in
its string formatting operations?

Thanks for your insights,

David


[code]

my_weight = 80 # kg

# 1 kg equals 2.20462262 pounds
print "My weight in pounds is %d"  % my_weight * 2.20462262

[/code]

python weight.py
Traceback (most recent call last):
  File "weight.py", line 4, in <module>
    print "My weight in pounds is %d"  % my_weight * 2.20462262
TypeError: can't multiply sequence by non-int of type 'float'



More information about the Tutor mailing list