Python E上的意外缩进

2024-09-30 00:42:14 发布

您现在位置:Python中文网/ 问答频道 /正文

有人能告诉我是什么导致了意外的缩进/预期的缩进错误吗?我不太明白,所以帮个忙就好了

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

Tags: thetoingowhichinputrawif
2条回答

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


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

I wanted the cluedoor = 1 to lead to a different statement

然后您将在该条件语句下编写语句:

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

“IndentationError”意味着解释器没有在预期的时候找到缩进(例如,在for或{})之后,或者在不需要的时候找到缩进。在

要解决问题,请替换:

if cluedoor == "1":
break

有:

^{pr2}$

但是请注意,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'

相关问题 更多 >

    热门问题