TypeError:/:'str'和'int'的不支持的操作数类型

fol*_*tor 4 python string int types typeerror

在Python 2.7中:

a=80
b=100

def status(hp, maxhp):
    print "You are at %r percent health." % hp*100/maxhp

status(a,b)
Run Code Online (Sandbox Code Playgroud)

返回:

TypeError:/:'str'和'int'的不支持的操作数类型

我已经尝试在每个变量和每个变量组合周围放置int().

Pav*_*sov 8

%运算符的优先级高于*/.

你的意思是:

"You are at %r percent health." % (hp * 100 / maxhp)
Run Code Online (Sandbox Code Playgroud)

你得到的是:

("You are at %r percent health." % hp) * 100 / maxhp
Run Code Online (Sandbox Code Playgroud)

 

编辑:实际上,我错了.它们具有相同的优先级,因此从左到右应用.

文档:运算符优先级