【发布时间】:2014-12-09 18:49:17
【问题描述】:
我正在写一个文字冒险(有人记得Zork吗?),但我在这段代码中遇到了麻烦:
from random import randint
def prompt():
action = input(">>> ").lower()
if action == "exit":
quit()
elif action == "save":
save()
else:
return action
def action_error(custom=False):
if custom != False:
print(custom)
else:
phrases = ["A bunch", "of funny", "error phrases"]
print(phrases[randint(1, len(phrases)-1)])
return prompt()
action = prompt()
while True:
print(action) #Debugging purposes
if action.find("switch") != -1:
if action.find("light") != -1:
second_room() #Story continues
else:
action = action_error("What do you want to switch?")
action = action_error()
问题是如果我输入一个包含“switch”的字符串,下一个输入就不会被拾取。
此外,任何人都有更好的方法来解析动词-名词字符串,例如“switch the light”、“open the door”或“look around”/“look at OBJECT”?
【问题讨论】:
-
我不知道我是否理解得很好,但
if "switch" in action是不是你想的那样? -
让我们看看我是否理解。您的预期行为是:用户输入“开关”。游戏打印“你想切换什么?”。用户输入“光”。游戏进入第二个房间。正确的?但是此刻,如果用户输入“开关”再输入“灯”,就没有按预期工作了?