pythongame-development

Get user input to determine an action and apply it to a specific Item


I'm working on making a basic text game. I'm trying to allow a player to type what they want to do and what they want to do it to. if the player wants to pickup shovel a shovel I want to check the action pickup then apply it to the item shovel.

So far I get AttributeError: 'list' object has no attribute 'find' when I run the game. I want to get this code to print only the shovel item when I type pickup shovel. I'm trying to make it more natural when trying to pick something up, instead of having to identify pickup and then ask what the player wants to pick up.

class Room(object):
    
    def __init__(self, name, description):
        self.name = name
        self.description = description
        self.exits = {}
        self.items = []
        

class Player(object):
    def __init__(self, name, location):
        self.name = name
        self.location = location
        self.inventory = []
        self.gameOver = False
        

class Item(object):
    def __init__(self, name, description, equipable, attackBonus, damage):
        self.name = name
        self.description = description
        self.equipable = equipable
        self.attackBonus = attackBonus
        self.damage = damage 

# Create Item
shovel = Item('Shovel', 'You find a sturdy shovel. It looks like it could do some good bashing.', True, 1, 2)
rockingChair = Item('Old Rocking Chair', "A rickety old rocking chair sits covered in grim. It doesn't look very appealing.", False, 0, 0)
stick = Item('Stick', 'You find a fragile looking stick, it looks like it could break easily.', True, 0, 0)
# Create Locations

YourHouse = Room("Your House", "There isn't anything new here.",)

TownHall = Room("Town Hall", "")
TownHall.items.append(shovel)
TownHall.items.append(rockingChair)
TownHall.items.append(stick)


Park = Room("Park", "There as some lovely green trees next to a small pond.")

# Create exits

YourHouse.exits = {"north": TownHall, "west": Park}
TownHall.exits = {'south': YourHouse,}

# Create the Player
player = Player('You are the Player', YourHouse)

# Start the Game

print('\n' + player.location.name)
print(player.location.description)
room_exits = "\n" + "The exits are: \n"
for character in room_exits:
    sys.stdout.write(character)
    sys.stdout.flush()
for exit in player.location.exits:
    print(exit)
while player.gameOver is False:


    # Define Player input
    action = input('>')

    # Movement
    if action.lower() in ['north', 'south', 'east', 'west']:
        if action in player.location.exits:
            player.location = player.location.exits[action]
            print("\n" "You went to " + player.location.name + '.')
        if len(player.location.description) <1:
            player.location.description = emptyDescription


    # Drop Item V2
    if action == 'drop':
        ask = input('What would you like to drop? \n')
        for ask in player.inventory:
            print('You drop the {}.'
                  .format(item.name))
            player.inventory.remove(item)
            player.location.items.append(item)


    # Examine area
    if action.lower() in ['examine', 'check', 'look']:
        for item in player.location.items:
            print('\n' + item.name)
            

    # Pick up Item
    # pickupCommand = ['pick up', 'get', 'take']
    if action.lower() in ['pickup', 'get', 'take']:
        ask = input('What would you like to pick up. \n')
        for item in player.location.items:
            # checks ask against items at playerlocation    
            for ask in player.location.items:
                if item.equipable:              
                    print("\n" "You have picked up a {}" "\n".format(item.name))
                    print("\n" + item.description + "\n")
                    # Remove Item from the room
                    player.location.items.remove(item)
                    # Add to Players Inventory
                    player.inventory.append(item)
                else:
                    print("You can't pick up this item.")
            else:
                print("That item isn't here.")


  

    # Check Players Inventory
    if action.lower() in ['inventory', 'inv', 'item', 'Item']:
        print("Your items are: ")
        for item in player.inventory:
            print(item.name)

    if action == 'exit':
        player.gameOver = True

Solution

  • In case the action is a string, and it probably is since you're using lower() on it, you can use action.startswith("pickup") to make sure the first word is that, or "pickup" in action if you want it to contain pickup. But you may want to look into regular expressions to make sure pickup is the whole word.