Python 3.2 Typeerror: Unsupported Operands(s) For %: 'nonetype' And 'str'
Just getting started with python and tried the following bit of code my_name = 'Joe Bloggs' my_age = 25 my_height = 71 # inches my_weight = 203 #lbs approximate, converted from ~14
Solution 1:
You want to move the close paren to the end of the line: print ("He's %d inches tall." % my_height)
That's because in Python 3, print
is a function, so you are applying the %
operator to the results of the print function, which is None
. What you want is to apply the %
operator to the format string and the string you wish to substitute, and then send the result of that operation to print()
.
EDIT: As GWW points out, this kind of string formatting has been deprecated in Python 3.1. You can find more information about str.format
, which replaces the %
operator, here: http://docs.python.org/library/stdtypes.html#str.format
However, since Python 2.x is the norm in most production environments, it's still useful to be familiar with the %
operator.
Post a Comment for "Python 3.2 Typeerror: Unsupported Operands(s) For %: 'nonetype' And 'str'"