2015-05-15 97 views
-2

我在elif myanswer ==「1」上收到ELIF錯誤:行 它說的語法錯誤然後指向elif的結尾。 感謝您的幫助,我真的很感激。無效語法elif python錯誤

#if statements to make the first scenario run 
print(place1) 
while not gameOver: 
    print(option1) 
    myanswer = getanswer() 
    elif myanswer == "1": 
     print("nothing happens, the dog just waits") 
    elif myanswer == "2": 
     print("You nobely decide to fight the dog.") 
#if statements to make the second scenario run 
     print(place2) 
     while not (belly and head and tail): 
      print(option2) 
      myanswer = getanswer(["1","2","3","4"]) 
      if myanswer == "1": 
       print("You scratched the dog on the head with your claws") 
       head = True 
      elif myanswer == "2": 
       print("You bite into the dog's soft belly") 
       belly = True 
      elif myanswer =="3": 
       print("You cut off the dog's tail with your razor sharp teeth") 
       tail = True 
      elif myanswer =="4": 
       print("You carefully retreat some steps away from the dog") 
       print(situation1) #return to the main loop 
       break # leave the dog loop     
       break # leave the dog loop and leave main loop 
     else: 
      # dog loop was leaved correctly 
      print("The dog bleeds from head, tail and belly and runs away." 
        "You are victorious! Congratulation, you have won the game") 
      gameOver = True #leave the dog loop correctly 
    elif myanswer == "3": 
     print("You run away, but the dog is faster than you and eats you whole!." 
       "The dog eat you. Game Over") 
     gameOver = True 
print("Try again and thanks for playing") 
+1

變化的第一個'elif'到'if',因爲你只能有一個'elif'是也有一個'if'聲明。 https://docs.python.org/2/tutorial/controlflow.html – martijnn2008

+0

因爲它'elif'具有** no **尾隨'if'條件,可以將其更改爲'if'或添加一個'if'語句它上面。此外,您在相同的縮進級別使用2個'break'語句,並且不會按預期工作。 – ZdaR

+0

你只能在if後寫elif。第一個應該由'if'替換 –

回答

0

elif只能用後if聲明。更換你的第一個elif

elif myanswer == "1" 

if myanswer == "1":