功能不调用另一个功能

时间:2013-12-18 00:09:51

标签: python

我有一个功能作为教程游戏的一部分。如果满足条件,有问题的函数应该触发另一个函数(如果对象=="代码")

# right room
def right_room():
    print "You see a table with two objects: a map and a code translator"
    print "You can take one object"
    print "Which object do you take?"

    next = raw_input("> ")

    if "map" in next and "code" in next:
        dead("You're greed surpassed your wisdom.")
    elif "map" in next:
        print "OK, you have the map."
        theobject = "map"
        print "Now you must exit and go ahead"
        return theobject
        opening()

    elif "code" in next:
        print "OK, you have the code."
        theobject = "code"
        print "Now you must exit and go ahead."
        return theobject
        opening()

但开场不叫?这是输出:

  

你在Labrynthe。你左边有一扇门。门上有一扇门   你的权利。或者你可以继续。

     
    

右边你看到一个有两个对象的表:一个地图和一个代码翻译你可以拿一个对象你拿哪个对象?     代码好的,你有代码。现在你必须退出并继续。

  

然后,上述功能意味着将人员发送回起点并提示他们输入"提前"在终端:

# opening scene
def opening():
    print "You're in a Labrynthe."
    print "There's a door on your left."
    print "There's a door on your right."
    print "Or you can go ahead."

    next = raw_input("> ")

    if "right" in next:
        right_room()
    elif "left" in next:
        left_room()
    elif "ahead" in next:
        ahead()
    else: 
        print "Which way will you go?"

但是没有调用opening()。相反,Python似乎完成了脚本并退出。

1 个答案:

答案 0 :(得分:2)

Python中的return语句(以及几乎所有语言 - Haskell的显着例外 - )意味着该函数应停止。如果语句为return expr,则表达式的值可供调用者使用。否则,在Python中,调用者可以使用值None

因此,您可能需要在return语句之前移动函数调用。