gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 22:37:03 26 4
gpt4 key购买 nike

这是我正在使用的一段代码:

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() == "豪宅":)

最佳答案

您可能想要稍微调整一下代码并使用变量来跟踪循环时用户“所在”的位置。像...

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

关于Python:一段脚本可以跳到另一段吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36958348/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com