2

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
cwallenpoole
82.4k26 gold badges132 silver badges174 bronze badges
asked Nov 4, 2011 at 5:19

2 Answers 2

3

There are two issues that stand out to me. First, use(...) is returning a boolean value, not the item used. No matter what the item used is, use(...) will return True. Second, in the eHouseAction method, you are testing to see if the value returned from showInv(...) is equal to True.

Since use(...) returns True and showInv(...) returns use(...), then a = showInv(...) is being set to True. eHouseAction is checking against True to open the door. Since using ANY item in your inventory will result in showInv(...) returning True, using ANY item in your inventory will open the door.

The solution is to make two changes:

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)

Now, it would probably be a good idea to place the used item back in the player's inventory if they do not try to use a house_key. Without that check, any item they use will disappear forever.

answered Nov 4, 2011 at 5:39
Sign up to request clarification or add additional context in comments.

1 Comment

I seemed to have lost sight of what my functions where returning and where they were returning to. I had a feeling that the answer would invoke a real face-to-palm moment. Thanks.
2

Well, I'm not sure how eHouseAccess could even work — you should be getting NameError: 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 to items[curItem][0]. Are you setting that correctly?


Other notes:

Inside of use (which is not the best name), you probably want a return statement before the first else clause.

I would also point this out as an issue in a code review:

if location == items[curItem][0]:

Why does that have a 0 index? It seems like putting some sort of data object there would make more sense. Then the code might look something like this:

if location == items[curItem].location:

Or better yet, make that location property a list of places where it can be used and the you can have:

if location in items[curItem].valid_locations:

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.

answered Nov 4, 2011 at 5:40

1 Comment

'items' was the dictionary I mentioned, which is referenced to see if the item is in the right location to be used. I didn't want to post all the variables, lists and dictionaries because it would have been 10X as long. Your notes were very helpful, and I have already made progress on a different issue I was having because of it. Thanks!

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.