Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6763717
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T14:30:34+00:00 2026-05-26T14:30:34+00:00

I am attempting to create a text-based adventure game for class, but I have

  • 0

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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T14:30:35+00:00Added an answer on May 26, 2026 at 2:30 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm attempting to create a text-based version of this game . Code: #include <iostream>
I am attempting to create a wrapper class for Google Android's Text-To-Speech functionality. However,
I wanted to create a text based browser game, so how should I go
I am attempting to create a simple program that will keep a text log
I attempting to create custom tabs using this . But when I try to
I'm attempting to create a generic controller, ie: public class MyController<T> : Controller where
I'm attempting to create a C++ plugin for a realtime 3D game. Although I
I'm attempting to create a custom DataGrid where I can format individual cells based
I'm attempting to create a RESTful service in CakePHP but I've hit a bit
Attempting to build an interface and generics based graph and getting an odd error

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.