Python 2.7の場合:
a=80
b=100
def status(hp, maxhp):
print "You are at %r percent health." % hp*100/maxhp
status(a,b)
戻り値:
TypeError:サポートされていないオペランドタイプ/:'str'および'int'
私はすでに、各変数と変数の各組み合わせの周りにint()を配置しようとしました。
%
*
演算子はまたはよりも優先され/
ます。
あなたが意味したのは:
"You are at %r percent health." % (hp * 100 / maxhp)
あなたが得たものは:
("You are at %r percent health." % hp) * 100 / maxhp
編集:実際、私は間違っています。それらは同じ優先順位を持っているため、左から右に適用されます。
式の周りに括弧をラップする必要があるため、結果を文字列に代入する前に、式が完全に評価されます。
何かのようなもの:
print "my string with this '%d' value" % (hp * 100 / maxhp)