2012-01-02 158 views
2

我只是貼在整個功能,因爲它不是長期無效的語法:與if語句

def decideTile(): 
    global tile 
    global exp 
    global maxexp 

    tile += 1 
    exp += math.ceil(random.randrange(math.ceil((maxexp/2)/2,maxexp/2)) 

    if exp >= maxexp: 
     levelUp() 
    else: 
     tileChoices = ['Battle','Loot','Nothing'] 
     fileType = random.choice(tileChoices) 

    if tileType == 'Battle': 
     battle() 
    elif tileType == 'Loot': 
     loot() 
    elif tileType == 'Nothing': 
     nothing() 

現在,Python是說,它的

if exp >= maxexp: 

部分是「無效的語法「,我不完全確定爲什麼。幫助表示讚賞!

+0

不要忘記接受最適合您的答案(點擊它的綠色複選標記)。對於更復雜的問題/答案,您可能需要等待幾個小時,然後選擇最完整的(可能不是第一個;)。 – 2012-01-02 07:39:16

+0

@ sbb0請接受正確的答案:)有關詳細信息,請參閱此問題:http://meta.stackexchange.com/q/5234/156122 – 2012-01-07 21:18:03

回答

10

上一行缺少一個括號。要解決這個問題,只需添加一個右括號到該行的末尾,如下所示:

exp += math.ceil(random.randrange(math.ceil((maxexp/2)/2,maxexp/2))) 
+0

我使用的程序強調了相應的括號,並且它們全部似乎已關閉... 奇怪的。尷尬的解決方案,但謝謝你。 – sbb0 2012-01-02 07:16:40

+1

jcollado是正確的。我建議使用Pyflakes來避免Python代碼中的常見缺陷。 – 2012-01-02 07:22:20

+0

是的,我第二[pyflakes](http://pypi.python.org/pypi/pyflakes)推薦。另外請注意,它可以與許多文本編輯器集成在一起,以便在編寫時檢查語法。 – jcollado 2012-01-02 07:24:20

0

未閉合的括號造成的錯誤。該行應該是:

exp += math.ceil(random.randrange(math.ceil((maxexp/2)/2,maxexp/2)))