Python错误意外缩进

时间:2014-07-28 01:53:44

标签: python indentation

有人能告诉我导致意外缩进的原因是什么/期望在此代码中出现缩进错误?我不太了解它,所以一些帮助会很好

print """77
"""
print '1. Go out through the door'
print '2. Stay in the room and find more clues'
option = raw_input('Which Option? ')
break
if option == "1":
    print """You attempt to open the door, but it is locked. After a few minutes of hopeless knob-turning, you finally give up. What do you do?
    """
    print '1. Stay in the room and find more clues'
    print '2. Attempt to use force to break the door'
    cluedoor = raw_input('Which Option? ')
    break
if option == "2":
    print """test123
    """
    print '1. Inspect the Rusted Cabinet'
    print '2. Go out in to the hallway'
    labhall = raw_input('Which Option? ')
    if labhall == "1": 
        print '222'
    if labhall == "2": 
        print '2'
    if cluedoor == "1":
    break
    test = raw_input("test")

2 个答案:

答案 0 :(得分:1)

“IndentationError”表示解释器在预期时未找到缩进(例如,在forif之后)或在未预期时发现缩进。

要解决您的问题,请替换:

if cluedoor == "1":
break

使用:

if cluedoor == "1":
    break

但请注意,cluedoor未在执行此if语句的部分中定义。通过一些重新排列,代码可以执行:

print '1. Go out through the door'
print '2. Stay in the room and find more clues'
option = raw_input('Which Option? ')
if option == "1":
    print """You attempt to open the door, but it is locked. After a few minutes of hopeless knob-turning, you finally give up. What do you do?
    """
    print '1. Stay in the room and find more clues'
    print '2. Attempt to use force to break the door'
    cluedoor = raw_input('Which Option? ')
    if cluedoor == "1":
        print "Staying in room"
    if cluedoor == "2":
        print "Attempting force"
if option == "2":
    print '1. Inspect the Rusted Cabinet'
    print '2. Go out in to the hallway'
    labhall = raw_input('Which Option? ')
    if labhall == "1":_
        print 'Going to rusted cabinet'
    if labhall == "2":_
        print 'Going to hallway'

答案 1 :(得分:0)

break语句单独在if块下无效(除非您发布的代码也位于[forwhile]循环内(在这种情况下,您应该发布所有代码。)。如果您希望程序只是在任何时候继续,请使用pass


你想要发生什么if cluedoor == "1":

  

我希望cluedoor = 1导致另一种说法

然后你将在条件陈述下写下声明:

if cluedoor == "1":
    print "a different statement"