-1

所以我正在尝试编写游戏代码并且我正在尝试连接关卡的不同区域。这部分代码应该允许用户选择进入关卡但终端不断给我这个错误命令:"expected an indented block".

我试图用四个空格替换所有选项卡,反之亦然,但错误不会消失。

顺便说一句,我知道在定义一个函数之后,每一行都会缩进四个空格或一个制表符,但这是我的第一个问题,我不知道如何在这里做到这一点。


def entry_hall():   
    first = raw_input("Go upstairs\nGo forwards\nGo left\nLeave\n:")
    if first == "Go upstairs" or "upstairs":
        print "Walking up the stairs" 
        import Upstairs_hallway.py
    elif first == "Go forwards" or "forwards":
        pass
    elif first == "Go left" or "left":
        pass
    elif first == "Leave":
        print """
You're a cop. You are not a baby. Do something else
""" 
        #restart the script

entry_hall()
4

3 回答 3

1

确保您的缩进是恒定的:

def entry_hall():   
    first = raw_input("Go upstairs\nGo forwards\nGo left\nLeave\n:")

    if first in ["Go upstairs", "upstairs"]:
        print "Walking up the stairs" 
        import Upstairs_hallway
    elif first == "Go forwards" or "forwards":
        pass
    elif first == "Go left" or "left":
        pass
    elif first == "Leave":
        print "You're a cop. You are not a baby. Do something else" 
        #restart the script

entry_hall()

此外,if first == "Go upstairs" or "upstairs"不会很好地工作。该语句将被评估为:

if (first == "Go upstairs") or "upstairs"

第一个条件 ,first == "Go upstairs"可以计算为TrueFalse,但第二个条件"upstairs", 将始终计算为True。因为您也在使用or语句,所以您的第一个条件将始终评估为True.

使用列表将解决此问题:

if first in ["Go upstairs", "upstairs"]:

此外,在 Python 中,import语句不希望有.py扩展。只需提供文件名:

import Upstairs_hallway
于 2012-08-26T18:55:16.007 回答
0

只需在每行代码后添加四个字符def

def entry_hall():   
    first = raw_input("Go upstairs\nGo forwards\nGo left\nLeave\n:")
于 2012-08-26T18:54:33.983 回答
0

不需要我的原始答案,因为您说def语句的错误对齐是由于不知道如何将其正确粘贴到 Markdown 中。

但是,您确实有另一个问题:您的if陈述是错误的。交换——前者永远为真,因为if s == 'first' or 'second':计算结果为真。if s in ['first', 'second']:'second'

于 2012-08-26T18:57:25.433 回答