2015-07-10 46 views
-6

我是一名初學者,目前正在學習Python。在構建經典遊戲「Pong!」時,我遇到了這個問題,在這個問題中,Python一直告訴我在elif語句中有一個語法錯誤。不過,我只是不能找出我的問題是...這裏是我的一些代碼:Python - 在'elif'語句中持續報告語法錯誤

def draw(canvas): 
    global score1, score2, pad1_pos, pad2_pos, ball_vel, RIGHT, c 
     # update ball 
    '''reflections on the left wall''' 
    if (ball_pos[0] == BALL_RADIUS + PAD_WIDTH) and (ball_pos in [range(pad1_pos, pad1_pos + PAD_HEIGHT)]): 
     print ball_pos 
     ball_vel[0] = - ball_vel[1] 
     ball_vel[1] = ball_vel[1] 
     c += 10 

    '''scoring of paddle1''' 
    elif (ball_pos[0] == BALL_RADIUS + PAD_WIDTH) and not (ball_pos in [range(pad1_pos, pad1_pos + PAD_HEIGHT)]): 
     score2 += 1 
     spawn_ball() 
     RIGHT = False 

    '''reflections on the right wall''' 
    elif ball_pos[0] == (WIDTH - 1) - BALL_RADIUS - PAD_WIDTH and (ball_pos in [range(pad2_pos, pad2_pos + PAD_HEIGHT)]): 
     '''remember how Python counts the values starting from 0''' 
     '''the first pixel of a 600p wall is 0 and the last one is 599''' 
     ball_vel[0] = - ball_vel[1] 
     ball_vel[1] = ball_vel [1] 
     c += 10 

    '''scoring of paddle2''' 
    elif ball_pos[0] == (WIDTH - 1) - BALL_RADIUS - PAD_WIDTH and not (ball_pos in [range(pad2_pos, pad2_pos + PAD_HEIGHT)]): 
     score1 += 1 
     spawn_ball() 
     RIGHT = True 

    '''reflections on the upper wall''' 
    elif ball_pos[1] == BALL_RADIUS: 
     ball_vel[0] = ball_vel[0] 
     ball_vel[1] = - ball_vel[1] 

    '''reflections on the bottom wall''' 
    elif ball_pos[1] == (HEIGHT - 1) - BALL_RADIUS: 
     ball_vel[0] = ball_vel[0] 
     ball_vel[1] = - ball_vel[1] 

我沒貼我的完整繪製函數,但這個問題在我的第一個elif的語句右邊出現說:「行85:SyntaxError:壞輸入('elif')」。我不知道我的語法錯誤在哪裏,奇怪的是我的if語句結果很好。我想知道如果我的elif聲明的內容是有效的,因爲它是一個相當長的一個,作爲一個初學者,我傾向於弄亂長代碼...

順便說一下,我在這裏要做的是基本上可以確定球何時應該反射,以及反射到何處。因此,我使用'in'和'range'語句來設置我的槳的範圍,以便球只會反射在槳上(範圍內)。我不確定這是否是正確的做法,請讓我知道是否在這裏搞亂了任何東西。這是我第一個發佈在堆棧上的第一個問題,我只是在徹底檢查了我的代碼之後才做出來的。對不起,這個問題結果超長,但我真的很感激,如果有人能幫助我。

+0

你好,歡迎來到StackOverflow。請花一些時間閱讀幫助頁面,尤其是名爲[「我可以詢問什麼主題?」(http://stackoverflow.com/help/on-topic)和[「我應該問什麼類型的問題避免問?「](http://stackoverflow.com/help/dont-ask)。更重要的是,請閱讀[Stack Overflow問題清單](http://meta.stackexchange.com/q/156810/204922)。您可能還想了解[最小,完整和可驗證示例](http://stackoverflow.com/help/mcve)。 –

回答

0

我同意,你應該考慮改變你的單行註釋使用英鎊的象徵。我運行了你的代碼,當我縮進多行註釋時,語法錯誤消失了。正如其他答案中提到的那樣,編譯器在if statement之後,預計要麼是一次縮進代碼,要麼是elif statement或與您的if具有相同縮進級別的新代碼。

def draw(canvas): 
    global score1, score2, pad1_pos, pad2_pos, ball_vel, RIGHT, c 
     # update ball 
    ''' 
    reflections on the left wall 
    ''' 
    if (ball_pos[0] == BALL_RADIUS + PAD_WIDTH) and (ball_pos in [range(pad1_pos, pad1_pos + PAD_HEIGHT)]): 
     print ball_pos 
     ball_vel[0] = - ball_vel[1] 
     ball_vel[1] = ball_vel[1] 
     c += 10 

    elif (ball_pos[0] == BALL_RADIUS + PAD_WIDTH) and (ball_pos in [range(pad1_pos, pad1_pos + PAD_HEIGHT)]): 
     print ball_pos 
     ball_vel[0] = - ball_vel[1] 
     ball_vel[1] = ball_vel[1] 
     c += 10 

    elif ball_pos[0] == (WIDTH - 1) - BALL_RADIUS - PAD_WIDTH and (ball_pos in range(pad2_pos, pad2_pos + PAD_HEIGHT)): 
     '''remember how Python counts the values starting from 0''' 
     '''the first pixel of a 600p wall is 0 and the last one is 599''' 
     ball_vel[0] = - ball_vel[1] 
     ball_vel[1] = ball_vel [1] 
     c += 10 

     '''scoring of paddle2''' 
    elif ball_pos[0] == (WIDTH - 1) - BALL_RADIUS - PAD_WIDTH and not (ball_pos in range(pad2_pos, pad2_pos + PAD_HEIGHT)): 
     score1 += 1 
     spawn_ball() 
     RIGHT = True 

     '''reflections on the upper wall''' 
    elif ball_pos[1] == BALL_RADIUS: 
     ball_vel[0] = ball_vel[0] 
     ball_vel[1] = - ball_vel[1] 

     '''reflections on the bottom wall''' 
    elif ball_pos[1] == (HEIGHT - 1) - BALL_RADIUS: 
     ball_vel[0] = ball_vel[0] 
     ball_vel[1] = - ball_vel[1] 
+0

非常感謝你!問題解決了。 –

6

an elif只能緊跟在if塊或elif塊之後。在ifelif之間有一個字符串常量'''scoring of paddle1''',所以嚴格來說,一個不會立即跟隨另一個。

考慮更改您的評論使用英鎊符號:#scoring of paddle1

+0

非常感謝兄弟!這真的有幫助 –