Kass D Kass D - 7 months ago 9
Python Question

Python: Can One Piece of Script Skip into another?

here is a piece of the code I am working with:

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


I was wondering how I make it so one part of the script (in this case,
if mansion in after_market:
) can take me to another part. (
elif second_do.lower() == "mansion":
)

Answer

You probably want to restructure you code a bit and use variables to keep track of where your user "is" while looping. Something like...

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

You can then go one step further and use functions for each location, e.g.

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

You could also have it be more controlled where someone in one place can go next, by having the functions return the new location:

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