我正在使用 python 开发基于文本的冒险游戏。现在我有它,所以主游戏循环总是打印“你想做什么?” 并将输入拆分为单个单词。
我有一个名为检查(也检查、观察、查看等)的操作,如果输入中有该单词,它将检查我在字典中描述的所有项目,如果找到,则打印项目的描述。
我怎样才能做到这一点,如果用户输入的单词都不在字典中,它会打印特定的消息?
#The dictionary of objects
objects = { #BUNCH OF DICTIONARY ENTRIES }
#The dictionary of checkable things other than the object list
check_dic = [{ #DICTIONARY },
{ #ANOTHER DICTIONARY },
{ #AND SO ON },
{ #AND SO FORTH}]
direct_dic = [{ #ANTOTHER LIST OF DICTIONARIES}]
#The beginning of the game.
print("Awaken adventurer, your journey begins here. What is your name?")
name = input('Enter your name:') #Sets the player's name.
print("Greetings, ", name, ". \nYou awaken in an unknown location equipped with only your trusty dagger, a strange vial, and a small pouch of gold coins.", sep= '')
player = "alive"
playerlocation = "room_1"
door1_state = "locked"
drawer_state = "locked"
trunk_state = "locked"
doorway_state = "locked"
plaque_state = "concealed"
bottle = "empty"
void_puzzle = 0
actions = ["check", "get", "drop", "use", "open", "orient", "go", "record", "read", "debugwarp"]
synonyms = ["look", "take", "examine", "observe", "pickup", "discard", "move"]
#The stone pile
s = 0
while player == "alive" and void_puzzle != "solved":
#print(playerlocation) #this is for checking what room the player is in at any time
if playerlocation == "room_1": #starting room
dictionary = check_dic[0]
items = items_in_room[0]
hidden = hidden_items[0]
directs = direct_dic[0]
orientation = orientations[0]
elif playerlocation == "room_2": #the rift room
dictionary = check_dic[1]
items = items_in_room[1]
hidden = hidden_items[1]
directs = direct_dic[1]
orientation = orientations[1]
#THERE'S A BUNCH OF LINES OMITTED THAT DO THE SAME THING FOR ALL OF THE OTHER ROOMS.
print("What do you want to do?")
userinput = input()
userinput = userinput.lower()
words = userinput.split()
if actions[0] in words or synonyms[0] in words or synonyms[2] in words or synonyms[3] in words:
for a in dictionary:
if a in words:
if a in dictionary:
if a == "drawer":
#A BUNCH OF STUFF RELATED SPECIFICALLY TO THE DRAWER
elif a == "desk":
#LIKEWISE
elif a == "trunk":
#ETC ETC THERE'S A BUNCH OF SPECIAL CASES HERE
else:
print(dictionary[a])
for b in objects:
if b in words:
if b in inventory:
print(objects[b])
else:
print("You can't check something you don't have.")
简而言之:我想要它,以便如果检查了单词并且输入不包含字典或对象中的任何内容,它会打印出像“你到底想检查什么?”这样的行。
有没有办法做到这一点?