Python:脚本可以跳到另一个脚本吗?

时间:2016-04-30 18:01:13

标签: python

这是我正在处理的一段代码:

if second_do.lower() == "market":
            print "You are now inside of the market place. It appears to be abandoned. The shelves are almost empty, however, you to manage find some salvagable goods, including peanut butter, beans, and crackers."
            goods = raw_input(">>> ")

            if goods.lower() == "collect":
                print "You have collected the food items. These will help you later on."
            if goods.lower() == "get":
                print "You have collected the food items. These will help you later on."
            if goods.lower() == "collect food":
                print "You have collected the food items. These will help you later on."
            if goods.lower() == "collect goods":
                print "You have collected the food items. These will help you later on."
            if goods.lower() == "get food":
                print "You have collected the food items. These will help you later on."
            if goods.lower() == "get goods":
                print "You have collected the food items. These will help you later on."

            after_market = raw_input("What's next?")

            if "mansion" in after_market:


elif second_do.lower() == "mansion":
            print "You are now inside of the mansion."

我想知道我是如何制作的,所以脚本的一部分(在这种情况下,if mansion in after_market:)可以带我到另一部分。 (elif second_do.lower() == "mansion":

2 个答案:

答案 0 :(得分:4)

您可能希望重新构建一些代码并使用变量来跟踪用户的位置""循环时。有点像...

location = "start"
while location != "exit":
    if location == "market":
        # do market related stuff
    elif location == "mansion":
        # do mansion related stuff

    location = raw_input("Where to next?")

然后,您可以更进一步,为每个位置使用函数,例如

def doMarket():
    # do market related stuff

def doMansion():
    # do mansion related stuff

location = "start"
while location != "exit":
    if location == "market":
        doMarket()
    elif location == "mansion":
        doMansion()

    location = raw_input("Where to next?")

通过让函数返回新位置,您也可以更好地控制一个地方的某个人可以进入的位置:

def doMarket():
    # do market related stuff

    # User can go anywhere from the market
    return raw_input("Where to next?")

def doMansion():
    # do mansion related stuff

    # User must always go to the market after the mansion
    return "market"

location = "start"
while location != "exit":
    if location == "market":
        location = doMarket()
    elif location == "mansion":
        location = doMansion()

答案 1 :(得分:0)

您需要做的最小改变是:

@GET("/stem/{funds}") Observable<Funds> getFunds(@Path("funds")

要使此代码为DRY,您必须将这些选项放在一个循环中,询问用户下一步该做什么,并且操作是为用户选择的选项执行的功能