如何修复“无效语法”错误?

时间:2012-12-04 09:24:40

标签: python syntax

这是整个计划。我已经写过我的屏幕上出现问题的地方。您可以从here(第6章,Dragon Realm)获取原始副本

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()

2 个答案:

答案 0 :(得分:3)

似乎已经掌握了python语法和语义的基础知识。 我建议您在没有首先掌握语言语法的基本知识的情况下停止尝试阅读代码,因为您将获得的所有内容都会让您感到困惑。

在我的诚实意见中,首先尝试在python中阅读一本书。两个伟大的(和免费的)Dive into PythonLearn Python the Hard way

答案 1 :(得分:2)

在函数定义choosepell之后,您似乎缺少冒号。魔术拼错了。尝试将其更改为:

  

def choosespell(魔法):在法术之间选择