dictionary - 移动和拾取基于项目的游戏(可能的 if-else 语句问题)
问题描述
我对python有点陌生,在我的课堂上做和分配制作一个基于文本的游戏,从一个房间移动到另一个房间,东南西北。除了第一个房间外,所有房间里都有玩家必须捡起的物品。移动的命令是'move "direction"',获取项目的命令是'get "item"'。我将房间设置在字典和每个房间的嵌套字典中,这些字典与相邻房间和每个房间中的项目相关。这是我当前的工作代码。
def show_instructions():
print('Collect 8 items to win the game and defeat the beast')
print('Move Commands: move North, move South, move West, move East, or Exit to leave the game')
print('Pickup items: get item, ex. get sword\n')
#Dictionary for rooms and Items
def main():
rooms = {
'Shed' : {'West': 'Well Room', 'item' : 'well room West of you'},
'Well Room' : {'South' : 'Sewers', 'East' : 'Shed', 'item' : 'Healing-Potion'},
'Sewers' : {'East' : 'Crawl Space','North' :'Well Room', 'item' : 'Armor'},
'Crawl Space' : {'East' : 'Cavern', 'West' : 'Sewers','item' : 'Sword'},
'Cavern' : {'South' : 'Dungeon Hall', 'West' : 'Crawl Space', 'item' : 'Shield'},
'Dungeon Hall' : {'East' : 'Hidden Room', 'West' : 'War Room', 'North' : 'Cavern', 'item' : 'Sword-Enchantment'},
'Hidden Room' : {'South' : 'Treasure Room', 'East' : 'Dungeon Hall', 'item' : 'Flame-Resistance-Potion'},
'War Room' : {'West' : 'Laboratory', 'East' : 'Dungeon Hall', 'item' : 'Armor-Enchantment'},
'Laboratory' : {'South' : 'Treasure Room', 'East' : 'War Room', 'Item' : 'Enchantment-Table'},
'Treasure Room' : {'item' : 'Dragon'}
}
#Helps with Decision branching
directions = ['North','South','East','West']
#Sets the player in the first room
currentroom = 'Shed'
inventory = []
print(show_instructions())
while currentroom != 'Exit':
def showStatus():
print('You are in the', currentroom)
print('Inventory:', inventory)
print('You see a', rooms[currentroom]['item'])
showStatus()
#Win Condition checkpoint
if currentroom == 'Treasure Room':
if len(inventory) < 8:
print('You get eaten by the Dragon')
break
else:
print('You slay the dragon and return to your boss victorious')
#input from player
c = input('What would you like to do?:\n')
#exit condition
if c == 'Exit':
currentroom = 'Exit'
#split to have tokens dignify if move or get item
tokens = c.split()
if tokens[0] == 'move' or 'Move':
if tokens[1] in directions:
try:
currentroom = rooms[currentroom][tokens[1]]
except KeyError:
print('You see a Wall')
else:
print('Not a Valid Direction')
elif tokens[0] == 'get' or 'Get':
if tokens[1] == rooms[currentroom]['item']:
inventory.append(rooms[currentroom]['item'])
else:
print('Not a valid Item')
else:
print('Not a Valid Entry')
print(main())
我遇到的问题是我是否输入 get 或 move 它会启动“if tokens[0] == 'move' or 'Move'”行。因此,键入 Get 只会打印“Not a Valid Direction”。谁能帮我看看我是如何弄乱我的陈述的?一天结束时,我希望我的移动可以从一个房间到另一个房间,打印房间里的物品,能够将物品添加到库存中,然后移动到下一个房间。一旦收集了 8 件物品,我想在到达“藏宝室”后赢得比赛。
解决方案
您的帖子格式错误,因此您应该真正尝试清理它。
一般来说,您的问题是语法之一。这一行:
if tokens[0] == 'move' or 'Move':
总是会评估为 True,因为or
是在 之后评估的==
,并且非空字符串的逻辑值是True
修复它的方法如下:
if tokens[0] == 'move' or tokens[0] == 'Move':
或者
if tokens[0] in ('move', 'Move'):
还有其他解决方案,但以上任何一个都应该没问题。
一旦这个问题得到解决,我希望你会发现更多。尝试尽可能地解决它们,并在必要时重新发布新问题。但请先尝试解决问题,并确保发布格式正确、完整的代码。
推荐阅读
- python - 如何使 Python 代码创建一个字典,该字典包含来自预先存在的字典的元素,该字典采用一个键和多个值?
- firebase - 使用 Nuxt 的 Firestore 规则和 Nuxt/firebase
- unix - 2 个远程 ssh 命令之间的 UNIX 管道通信
- javascript - 在 .map 方法中引用函数
- python - History-Dependent List-Comprehension 或如何表达`[f(x, this) for x in X if g(x, this)]`?
- python - 为什么我的输出会执行多个打印功能?
- javascript - 为什么使用切换产生器对数组进行排序与没有切换产生不同的下降结果?
- python - 如何构建一个基本的深度学习项目以便在 django 中使用它
- c# - 具有嵌套对象和容器类的 AutoMapper
- r - 如何在R中指定可以联合或单独子集的多个条件