Python 2.7 如何使代码跳到if语句中的某一点
我正在做一个基本的游戏。我想知道如何使代码转到code/if语句中的特定点,而不是从一开始就开始 例如,在我的代码第19行:Python 2.7 如何使代码跳到if语句中的某一点,python-2.7,if-statement,Python 2.7,If Statement,我正在做一个基本的游戏。我想知道如何使代码转到code/if语句中的特定点,而不是从一开始就开始 例如,在我的代码第19行:kithcn()我不想将它重定向到if语句的开头;再次进入描述厨房,然后要求输入:choice02=raw\u input(“你做什么?”),但我希望它直接跳到输入部分 def kitchen(): print "You see a dusty old kitchen, nobody has probably used it for quite a while."
kithcn()
我不想将它重定向到if语句的开头;再次进入描述厨房,然后要求输入:choice02=raw\u input(“你做什么?”)
,但我希望它直接跳到输入部分
def kitchen():
print "You see a dusty old kitchen, nobody has probably used it for quite a while."
print "There are 3 different cupboards(0, 1, 2) or a letter(3) placed on the counter."
choice02 = raw_input("What do you do? >")
if choice02 == "0" or choice02 == "first cupboard" or choice02 == "check first cupboard" or choice02 == "open first cupboard":
print "You see a dusty empty bottles of milks, with spiderwebs at the corners of the cupboard. Nothing of Interest here."
raw_input(">")
kitchen()
elif choice02 == "1" or choice02 == "second cupbaord" or choice02 == "check second cupboard" or choice02 == "open second cupboard":
print "You see some packs of likely expired cereal and a key(0)."
choice_02_A = raw_input("What do you do? >")
#----------------------------------------------------------------------------------------------------------#
if choice02_A == "pick key" or choice02_A == "key" or choice02_A == "0" or choice02_A == "get key":
print "You picked the key. This probably unlocks some door."
raw_input("> ")
kitchen()
elif choice02_A == "close cupboard" or choice02_A == "kitchen" or choice02_A == "go back to kitchen":
kitchen()
else:
print "Not a valid option."
#-----------------------------------------------------------------------------------------------------------#
elif choice02 == "2" or choice02 == "third cupboard" or choice02 == "check third cupboard" or choice02 == "open third cupboard":
print "You see an empty or dusty cupboard. Nothing of interest here."
raw_input("> ")
kitchen()
elif choice02 == "3" or choice02 == "check letter" or choice02 == "letter" or choice02 == "read letter":
print """
You read the letter:
\n"Dear Shawn............\n"
It makes no sense to you.
"""
elif choice02 == "go back" or choice02 == "entrance hall" or choice02 == "go to entrance hall":
entrance_hall()
else:
"Not a valid Option."
我看不到循环,但如果您在从外部调用函数时只需要print语句,则可以使用如下可选布尔值:
def kitchen(already_there=False):
if not already_there:
print('...')
choice02 = raw_input("What do you do? >")
# ...
elif # ...
# ...
kitchen(already_there=True)
您可能需要考虑重构代码并使用字典避免长IF语句。
看适当缩进你的代码同样,你在问题中不断地说“循环”。什么循环?对不起,我的错,我是新来的。一个误解,不管怎样,我做了一个编辑。复印时缩进有点乱。不用担心。另外,请缩进。目前的代码不是有效的Python。特别是,在
def
之后的所有内容都需要升级一级。这特别好,因为它与您已经编写的任何代码都向后兼容。好的,谢谢!这肯定更干净。尽管我如何为一个值/stringHmm分配多个键,但一种可能性是使用两个字典。一个用于存储别名密钥关联,另一个用于存储实际数据。我很快会更新我的答案。
print "There are 3 different cupboards(0, 1, 2) or a letter(3) placed on the counter."
alias = {
'0': '0',
'first cupboard': '0',
'check first cupboard': '0',
'open first cupboard': '0',
'1': '1',
'second cupboard': '1',
'check second cupboard':'1',
...
}
dic = {
"0":"You see a dusty empty bottles of milks, with spiderwebs at the corners of the cupboard. Nothing of Interest here.", \
"1":"You see some packs of likely expired cereal and a key(0).", \
"2":"You see an empty or dusty cupboard. Nothing of interest here."
...
}
while(True):
choice02 = raw_input("What do you do? >")
if choice02 in alias:
print dic[alias[choice02]]
break
else:
print "Not a valid option"