我一直坚持这个我一直在试验的基本程序。
这是我的代码:
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。
答案 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)