eof解析时出现意外错误

时间:2015-01-24 16:36:28

标签: python parsing eof

我的代码不起作用,我无法解决它,请帮忙。它是最后一行代码

抱歉,我不知道怎么把它放在其中一个盒子里(我对编码和python很新)

def start():
    print ("Type start.")
    prompt_sta()
    print

def prompt_sta ():
    prompt_0 = raw_input ("Enter command:    ")
    try:
        if prompt_0 == "start":
            outside_house ()
        elif prompt_0 == "begin":
            print ("Know when to follow instructions...")
            print
            prompt_sta ()
        elif prompt_0 == "help":
            print ("""Commands: help start begin""")
            prompt_sta ()
        else:
            print ("You must enter a valid command.")
            print
            prompt_sta ()
    except ValueError:
        "Type start."
        print
        print (prompt_sta ())

def outside_house ():
    print ("You are outside a small house.")

def prompt_outside ():
    prompt_1 = raw_input ("Enter Command:    ")
    try:
        if prompt_0 == "enter":
            inside_house ()
        elif prompt_0 == "help":
            print ("""Commands:    enter    help""")
            prompt_outside ()
        else:
            print ("Type a valid command.")
            print
            prompt_outside ()

1 个答案:

答案 0 :(得分:1)

对于每个try,您需要一个except

def prompt_outside ():
    prompt_1 = raw_input ("Enter Command:    ")
    try:
        ...
    except:
        ...

错误来自于except之后没有try。你可以有这样的东西,例如:

def prompt_outside ():
    prompt_1 = raw_input ("Enter Command:    ")
    try:
        if prompt_0 == "enter":
            inside_house ()
        elif prompt_0 == "help":
            print ("""Commands:    enter    help""")
            prompt_outside ()
        else:
            print ("Type a valid command.")
            print
            prompt_outside ()
    except:
        print 'exception'