Note that there are some explanatory texts on larger screens.

plurals
  1. POpython: functions
    primarykey
    data
    text
    <p>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. </p> <p>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. </p> <p>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. </p> <p>Any help would be appreciated here, whether to the specific question or anything that you might do differently. </p> <p>These are the functions in question:</p> <pre><code>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("&gt; ")) 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 </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload