我正在写一篇文字冒险(有人记得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”?