加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

python – TypeError:/:’str’和’int’的不支持的操作数类型

发布时间:2020-12-20 11:24:58 所属栏目:Python 来源:网络整理
导读:在 Python 2.7中: a=80b=100def status(hp,maxhp): print "You are at %r percent health." % hp*100/maxhpstatus(a,b) 返回: TypeError: unsupported operand type(s) for /: ‘str’ and ‘int’ 我已经尝试在每个变量和每个变量组合周围放置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: unsupported operand type(s) for /: ‘str’ and ‘int’

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

解决方法

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

你的意思是:

"You are at %r percent health." % (hp * 100 / maxhp)

你得到的是:

("You are at %r percent health." % hp) * 100 / maxhp

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

Docs: operator precedence

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读