TypeError:/:'str'和'int'的不支持的操作数类型 [英] TypeError: unsupported operand type(s) for /: 'str' and 'int'
本文介绍了TypeError:/:'str'和'int'的不支持的操作数类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在Python 2.7中:
In 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'不支持的操作数类型
TypeError: unsupported operand type(s) for /: 'str' and 'int'
我已经尝试在每个变量和每个变量组合周围放置int()。
I've already tried putting int() around each variable and each combination of variables.
推荐答案
%
运算符的优先级高于 *
或 /
。
%
operator has higher precedence than *
or /
.
你的意思是:
"You are at %r percent health." % (hp * 100 / maxhp)
你得到的是:
("You are at %r percent health." % hp) * 100 / maxhp
编辑:实际上,我错了。它们具有相同的优先级,因此从左到右应用。
actually, I'm wrong. They have the same precedence and thus are applied left to right.
这篇关于TypeError:/:'str'和'int'的不支持的操作数类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文