2012-12-04 190 views
-4

這是整個程序。我已經寫過問題出現在我的屏幕上。您可以從here拿到正本(第6章,龍之王國)如何解決「無效語法」錯誤?

import random 
import time 

def displayIntro(): 
    print('You are in a land full of dragons. In front of you,') 
    print('you see two caves. In one cave, the dragon is friendly') 
    print('and will share his treasure with you. The goblin') 
    print('is greedy and wants to loot you on sight.') 
    print() 

def chooseCave(): 
    cave = '' 
    while cave != '1' and cave != '2': 
     print('Which cave will you go into? (1 or 2)') 
     cave = input() 

    return cave 

def checkCave(chosenCave): 
    print('You approach the cave...') 
    time.sleep(2) 
    print('It is dark and spooky...') 
    time.sleep(2) 
    print('The goblin sees you and tells you too hand over your stuff') 
    time.sleep(2) 
    print("oh! I forgot you are an apprentice looking Merlin you can use ur magic on him") 
    time.sleep(1) 
    print("You can choose betweeen, fire, water, air or earth") 
    print() 
    time.sleep(2) 

    friendlyCave = random.randint(1, 2) 

    if chosenCave == str(friendlyCave): 
     print('Gives you his treasure!') 
    else: 
     print('Takes your stuff and run') 
def spell(magic): 

這是什麼地方,我認爲出現問題。

if magic == 'air' or magic == 'fire' or magic == 'water' or magic == 'earth' #This is where the problem occurs I think it is because of some prior codes 
def choosespell(maguc)#choose between spells 
if magic == 'air': 
    print('you blew the goblin away') 
    elif magic == 'fire': 
     print('you burned the goblin to death') 
     elif magic == 'water': 
      print('the goblin drowned to death') 
      elif magic == 'earth': 
       print('The veins chocked him to death') 
magic = input() 
playAgain = 'yes' 
while playAgain == 'yes' or playAgain == 'y': 

    displayIntro() 

    caveNumber = chooseCave() 

    checkCave(caveNumber) 

    print('Do you want to play again? (yes or no)') 
    playAgain = input() 
+3

錯誤的縮進。你知道領先空間在Python中扮演着至關重要的角色嗎? – Matthias

+1

你*認爲*這是問題發生的地方?你有沒有檢查錯誤信息中提到的行號? – Junuxx

+2

第二個代碼塊包含很多錯誤,因此很難知道您是否在問題中插入文本時做得不好,或者如果這確實是您擁有的代碼。請確保問題中的代碼與編輯器中的代碼相同。正如Matthias所說;在python中縮進非常重要! – Markus

回答

2

它看起來像你在函數定義選擇pepe後丟失冒號。另外魔法拼寫錯誤。嘗試將其更改爲:

高清choosespell(魔術):法術

+1

上面那條線也錯過了一個冒號,並且有一個聲明去追蹤它。 – Junuxx

3

之間#choose你做似乎已經掌握了Python語法和語義的基礎知識。 我建議你不要先閱讀代碼,先閱讀代碼,而不要先掌握一些關於語言語法的基本知識,因爲你所得到的所有東西都會感到困惑。

在我的老實說法中,首先嚐試用python讀一本書。兩個偉大的(和免費的)Dive into PythonLearn Python the Hard way