获取错误:在字符串格式化过程中并非所有参

时间:2017-08-17 03:01:16

标签: python

我一直坚持这个我一直在试验的基本程序。

这是我的代码:

def description1(self):
    desc = "%s is a %s, attack %s , health %s , defence %s , speed $s"%(self.name, self.description, self.attack, self.healthPoints, self.defence, self.speed)
    return desc

以下是错误消息:

   line 9, in description1
    desc = "%s is a %s, attack %s , heath %s , defence %s , speed $s"%(self.name, self.description, self.attack, self.healthPoints, self.defence, self.speed)
TypeError: not all arguments converted during string formatting

我使用的是Python 3.5。

1 个答案:

答案 0 :(得分:0)

你的格式%s的错误输入错过了。 下面是具有正确缩进的代码

class playerStats:
    name = ""
    description = ""
    attack = ""
    healthPoints = ""
    defence = ""
    speed = ""

    def description1(self):
        desc = "%s is a %s, attack %s , health %s , defence %s , speed %s"%(self.name, self.description, self.attack, self.healthPoints, self.defence, self.speed)
        return desc


#Defining playerstats for each of the Characters
stickNerd = playerStats()
stickNerd.name = "Stick Nerd"
stickNerd.description = "Nerd that only dreams of a 101%"
stickNerd.attack = "10"
stickNerd.healthPoints = "5"
stickNerd.defence = "5"
stickNerd.speed = "8"


print(stickNerd.description1())

输出

Stick Nerd is a Nerd that only dreams of a 101%, attack 10 , health 5 , defence 5 , speed 8

即使你可以使用

desc = "{name} is a {description}, attack {attack} , health {healthPoints} , defence {defence} , speed {speed}".format (name=self.name, description=self.description, attack=self.attack, healthPoints=self.healthPoints, defence=self.defence, speed=self.speed)