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

2024-09-30 00:39: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

以下是错误消息:

^{pr2}$

我使用的是python3.5。在


Tags: 代码nameself程序returnisdefdescription
1条回答
网友
1楼 · 发布于 2024-09-30 00:39:16

您的格式有误%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())

输出

^{pr2}$

你也可以用

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)

相关问题 更多 >

    热门问题