类型错误:%d 格式:需要一个数字,而不是 str [英] TypeError: %d format: a number is required, not str

查看:79
本文介绍了类型错误:%d 格式:需要一个数字,而不是 str的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用打印格式打印字符串和数字,我有以下代码:

I want to use print format to print strings and numbers, and I have the following code:

John = {'strength': 10, 'skill': 10}
character1 = "John"
strength = eval(character1)['strength']
skill = eval(character1)['skill']
print " %d " % eval(character1)['strength']
print " %d " % strength
print "$s's strength is: %d, and $s's skill is: %d" % (character1, strength, character1, skill)
print "$s's strength is: %d, and $s's skill is: %d" % character1, eval(character1)['strength'], character1, eval(character1)['skill']

前两个打印我没有错误,他们打印 10,最后两个打印给我:类型错误:%d 格式:需要数字,而不是 str".

The first two print I have no error, they print 10, the the last two print give me: "TypeError: %d format: a number is required, not str".

不明白为什么,请指点,谢谢!

I do not understand why, please give some pointers, thanks!

推荐答案

这里有错误:$s 应该是 %s.

Here's the mistake: $s should be %s.

这篇关于类型错误:%d 格式:需要一个数字,而不是 str的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆