函数不调用其他函数

2024-05-12 19:49:11 发布

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

我有一个函数作为一个教程游戏的一部分。如果满足某个条件(如果对象==“code”),则该函数应触发另一个函数

# 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()

但开放不叫开放吗?以下是输出:

You're in a Labrynthe. There's a door on your left. There's a door on your right. Or you can go ahead.

right You see a table with two objects: a map and a code translator You can take one object Which object do you take? code OK, you have the code. Now you must exit and go ahead.

然后,上面的功能意味着将人员发送回起始位置,并提示他们在终端中输入“ahead”:

# 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似乎完成了脚本并退出了。你知道吗


Tags: andinrightyougomapyourcode
1条回答
网友
1楼 · 发布于 2024-05-12 19:49:11

Python中的return语句(几乎所有语言都是Haskell的显著例外)意味着函数应该在那里停止。如果语句是return expr,则表达式的值可供调用方使用。否则,在Python中,调用者可以使用值None。你知道吗

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

相关问题 更多 >