2012-12-04 188 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