2013-02-04 30 views
0

我一直在關注this系列YouTube視頻,以獲得更多的Python方法。我對代碼的某些功能沒有深入的瞭解,但是我或多或少地得到了每件作品應該實現的功能,儘管我可能不知道如何實現。Python代碼在「if self.hp <0」行上返回語法錯誤

我得到的最後一行這裏語法錯誤:

class Character(object): 
def __init__(self, name, hp): 
    self.name = name 
    self.hp = hp 

    self.dead = False 
def attack(self, other): 
    pass 

def update(self): 
    if self.hp < 0  #Error's on this line 
     self.dead = True 
     self.hp = 0 

這裏是回溯:

Traceback (most recent call last): File "game.py", line 4, in 
<module> 
    from Characters.player import * File "/Users/Devlin/Desktop/Dev/Python/rpg/Characters/player.py", line 2, 
in <module> 
    from character import * File "/Users/Devlin/Desktop/Dev/Python/rpg/Characters/character.py", line 
12 
    if self.hp < 0 
       ^SyntaxError: invalid syntax 
+0

添加':',如果self.hp <0:' –

回答

3

你忘了在該行的末尾有一個冒號(:):

if hp < 0: 
+0

呵呵。我不覺得傻。感謝您的幫助,修復了這個問題:) – spoonless

1

init外觀像一個構造函數。因此,它應該可能是__init__而不是init。否則,你會遇到問題,它不會在對象實例化時被執行。

+0

目前我只是跟着那個教程一起學習。我嘗試了你的建議,並返回一個TypeError('TypeError:object .__ init __()不帶參數)。我是否需要回到代碼的其他部分並相應地進行調整? – spoonless