2017-08-17 75 views
0

我一直堅持這個我一直在試驗的基本程序。獲取錯誤:並非所有在字符串格式化過程中轉換的參數

這裏是我的代碼:

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

你'速度$ s',推測你的意思'%s'。由於錯字你只有5'%s',但元組中有6項 - 因此是錯誤。 – AChampion

+0

此外,縮進全部關閉 – AetherUnbound

回答

0

錯字輸入格式錯字。 下面是一個正確的縮進

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) 
+0

謝謝。我想下一次我會更加關注 – Moose

相關問題