python:函数
我正在尝试为班级创建一个基于文本的冒险游戏,但在使用库存中的物品时,我遇到了让“使用”功能正常工作的问题 游戏有两个交互菜单,主菜单(您可以在其中旅行、搜索、显示和使用资源清册、显示ASCII地图或退出)和旅行菜单(在不同地点之间移动的while循环)。我想将其中一扇门锁定到某个位置,要求用户在不同的位置找到钥匙(从而将其添加到库存),然后在提示访问该位置时从库存中选择该项 我创建了一个字典供use函数引用,以验证该项是否可以在特定位置使用。我测试了函数中的逻辑,它工作了。但是,出于某种原因,它会接受门上使用的任何物品,我认为这与功能之间的相互处理方式有关,因为使用功能由show inventory功能调用 任何帮助,无论是针对具体问题还是您可能会采取的不同做法,都将不胜感激 这些是有关的职能:python:函数,python,Python,我正在尝试为班级创建一个基于文本的冒险游戏,但在使用库存中的物品时,我遇到了让“使用”功能正常工作的问题 游戏有两个交互菜单,主菜单(您可以在其中旅行、搜索、显示和使用资源清册、显示ASCII地图或退出)和旅行菜单(在不同地点之间移动的while循环)。我想将其中一扇门锁定到某个位置,要求用户在不同的位置找到钥匙(从而将其添加到库存),然后在提示访问该位置时从库存中选择该项 我创建了一个字典供use函数引用,以验证该项是否可以在特定位置使用。我测试了函数中的逻辑,它工作了。但是,出于某种原因,它
def eHouseAccess(action, location, oldLocation): # called by travel to validate if the location is accessable
global eHouse
if eHouse == 'locked' and inventory == []:
print "The door is locked! You need to find a key for this door."
travel(oldLocation)
elif eHouse == 'locked':
print "The door is locked! You need to find a key for this door."
print "Maybe you have it in your inventory?"
a = showInv(inventory, location, items)
if a == True:
eHouse = 'open'
travel(location)
else:
travel(oldLocation)
else:
location = travelOpt[location][action - 1]
travel(location)
def travel(location):
while True:
print "You are in the", location[0]+"."
print location[1]
print 'You can travel to:'
for (i, t) in enumerate(travelOpt[location]):
print i + 1, t[0]
action = raw_input("Pick a destination, or enter 'menu' for the main menu: ")
if action == 'menu':
main_menu(location, inventory, items)
else:
action = int(action)
if travelOpt[location][action - 1] == engineer_house:
oldLocation = location
location = engineer_house
eAccess = eHouseAccess(action, location, oldLocation)
elif travelOpt[location][action - 1] == castle_inside:
cInside = cInsideAccess(action, location, cInside)
else:
location = travelOpt[location][action - 1]
def main_menu(location, inventory, items):
print "You are in the", location[0] + "."
menu_list = ['Travel', 'Inventory', 'Search', 'Map', 'Quit']
print "Choose one:"
for (num, t) in enumerate(menu_list):
print num + 1, t
main_choice = int(raw_input("> "))
action = menu_list[main_choice - 1]
if action == 'Travel':
travel(location)
elif action == 'Inventory':
showInv(inventory, location, items)
elif action == 'Search':
search(location, inventory, items)
elif action == 'Map':
map(location)
elif action == 'Quit':
exit()
else:
print "That is not a valid option!"
main_menu(location, inventory, items)
def showInv(inventory, location, items):
if inventory == []:
print "Your inventory is EMPTY"
sInv = raw_input("Hit 'enter' to return to the 'main menu': ")
main_menu(location, inventory, items)
else:
print "These 'items' are in your 'inventory':"
for (num, i) in enumerate(inventory):
print num + 1, i
sInv = raw_input("Type 'menu' to return to the main menu or 'use' to use and item: ")
if sInv == 'menu':
main_menu(location, inventory, items)
if sInv == 'use':
a = use(items, inventory, location)
return a
else:
print "That is not a valid entry!"
showInv(inventory, location, items)
def use(items, inventory, location):
if inventory == []:
print "There is nothing to use."
invEmpty = raw_input("Hit 'enter' to return to the 'main menu': ")
main_menu(location, inventory, items)
else:
uItem = int(raw_input("Choose an item to use: "))
curItem = inventory[uItem - 1]
if location == items[curItem][0]:
print "You used", inventory[uItem - 1]+"."
inventory.pop(uItem -1)
main_menu(location, inventory, items)
return True
else:
print "You cannot use that here!"
main_menu(location, inventory, items)
return False
有两个问题对我来说很突出。首先,use(…)返回的是布尔值,而不是所使用的项。无论使用的项是什么,use(…)都将返回True。其次,在eHouseAction方法中,您正在测试showInv(…)返回的值是否等于True 由于use(…)返回True,而showInv(…)返回use(…),因此a=showInv(…)被设置为True。eHouse Action正在检查是否为True以打开门。由于使用库存中的任何项目都会导致showInv(…)返回True,因此使用库存中的任何项目都会打开大门 解决方案是进行两项更改:
def eHouseAccess(action, location, oldLocation):
[snip]
a = showInv(inventory, location, items)
if a == house_key: # I'm not exactly sure what you called the key
eHouse = 'open'
travel(location)
def use(items, inventory, location):
[snip]
if location == items[curItem][0]:
print "You used", inventory[uItem - 1]+"."
return inventory.pop(uItem -1)
现在,如果玩家不尝试使用房屋钥匙,最好将使用过的物品放回玩家的库存中。如果没有该检查,他们使用的任何项目都将永远消失。好吧,我不确定
eHouseAccess
如何工作-您应该获得NameError:未定义全局名称“项目”
。您可能想要,全球电子房屋,物品
。我猜,你的bug与工程师之家有关。您正在尝试将其与项[curItem][0]
进行比较。你的设置正确吗
其他说明: 在
use
(这不是最好的名称)的内部,您可能希望在第一个else
子句之前有一个return
语句
我还想在代码审查中指出这一点:
if location == items[curItem][0]:
为什么它有一个0
索引?似乎在那里放置某种数据对象更有意义。然后,代码可能如下所示:
if location == items[curItem].location:
或者更好的做法是,将该位置属性列为可以使用的位置列表,以及您可以拥有的:
if location in items[curItem].valid_locations:
当然,我仍然会返回所选对象,而不是返回是否可以使用它。否则,如果你有两件或两件以上的事情可以做,那么你可能会不小心用肥皂刷牙。“items”是我提到的字典,它被引用以查看项目是否位于正确的使用位置。我不想发布所有的变量、列表和字典,因为它的长度是原来的10倍。你的笔记非常有用,我已经在一个不同的问题上取得了进展。谢谢我似乎已经看不清我的职能在哪里以及它们在哪里。我有一种感觉,这个答案会唤起一个真实的面对面的瞬间。谢谢