繁体   English   中英

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

[英]TypeError: unsupported operand type(s) for /: 'str' and 'int'

在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

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

文档:运算符优先级

你需要在表达式周围包装parens,所以在尝试将结果替换为字符串之前,它会被完全评估。

就像是:

print "my string with this '%d' value" % (hp * 100 / maxhp)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2025 STACKOOM.COM