I am attempting to create a text-based adventure game for class, but I have been experiencing problems in getting the ‘use’ function to work when using an item in inventory.
The game has 2 interactive menus, the main menu (where you can travel, search, display and use inventory, display an ASCII map, or quit) and the travel menu (a while loop that moves you between locations). I wanted to lock one of the doors to a location, requiring the user to find a key in a different location (thereby adding it to inventory) and then choose that item from inventory when prompted to gain access to the location.
I created a dictionary to be referenced by the use function to verify if the item can be used in a specific location. I tested the logic within the function and it worked. However, it will accept any item to be used on the door for some reason and I think it has to do with the way the functions deal with each other seeing as the use function is called on by the show inventory function.
Any help would be appreciated here, whether to the specific question or anything that you might do differently.
These are the functions in question:
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
Well, I’m not sure how
eHouseAccesscould even work — you should be gettingNameError: global name 'items' is not defined. You probably wanted,global eHouse, items. Your bug, my guess, has to do with engineer_house. You are trying to compare it toitems[curItem][0]. Are you setting that correctly?Other notes:
Inside of
use(which is not the best name), you probably want areturnstatement before the firstelseclause.I would also point this out as an issue in a code review:
Why does that have a
0index? It seems like putting some sort of data object there would make more sense. Then the code might look something like this:Or better yet, make that location property a list of places where it can be used and the you can have:
Of course, I would still return the object selected and not whether or not it could be used. Otherwise, in a situation where you have two or more things you can do, then you could accidentally brush your teeth with hand soap.