I'm trying to take user input and find it in a list of items.
I can move to a location succesfully, and I can ask a player what they want to pick up.
When the code gets to if ask in myPlayer.location.items[item.name]:
I get the following error.
#MARK: Imports
import sys
#MARK: Classes
class Player(object):
def __init__(self, name, location, job):
self.name = name
self.location = location
self.job = job
self.inventory = []
self.gameOver = False
class Room(object):
def __init__(self, name, description):
self.name = name
self.description = description
self.exits = {}
self.items = []
class Item(object):
def __init__(self, name, description):
self.name = name
self.description = description
class Job(object):
def __init__(self, name, health):
self.name = name
self.health = health
#MARK: Locations
yourHouse = Room("Your House", "This is your small house. Nothing has changed here")
townHall = Room("Town Hall", "This is the Town Hall. Sometimes people gather here to make decissions for the town")
townPark = Room("Town Park", "This is the towns small Park.\nThere is a small pond with a park bench on it's edge where you can sit and feed the ducks.\nA few trees provide shade.")
townGeneralStore = Room("The General Store", "The General Store is the largest building in town. Here you can buy almost everything you need.")
#MARK: Exits
yourHouse.exits = {'north': townHall, 'west': townPark}
townHall.exits = {'south': yourHouse, 'west': townGeneralStore}
townPark.exits = {'east': yourHouse, 'north': townGeneralStore}
townGeneralStore.exits = {'south': townPark, 'east': townHall}
#MARK: Jobs
fighter = Job("Fighter", 40)
sneak = Job("Sneak", 20)
#MARK: Create player
myPlayer = Player("", yourHouse, "")
#MARK: Create items
shovel = Item('Shovel', "You find a sturdy shovel. It looks like it could do some good bashing.")
stick = Item('Stick', 'You find a fragile looking stick, it looks like it could break easily.')
#MARK: Place Items
townHall.items.append(shovel)
townPark.items.append(stick)
print('\n' + myPlayer.location.name)
print(myPlayer.location.description)
room_exits = "\n" + "The exits are: \n"
for character in room_exits:
sys.stdout.write(character)
sys.stdout.flush()
for exit in myPlayer.location.exits:
print(exit)
#MARK: Main game loop
while myPlayer.gameOver is False:
# Start player in their house
action = input('>')
plaAction = action.lower()
# Movement
if plaAction in ['north', 'south', 'east', 'west']:
if plaAction in myPlayer.location.exits:
myPlayer.location = myPlayer.location.exits[action]
print("\n" "You went to " + myPlayer.location.name + '.')
print("\n" + myPlayer.location.description)
for exit in myPlayer.location.exits:
print(exit)
# Pick up items
if plaAction in ['get']:
ask = input("What would you like to pick up?\n")
print(type(ask))
print(type(myPlayer.location.items))
for item in myPlayer.location.items:
if ask in myPlayer.location.items[item.name]:
myPlayer.inventory.append(ask)
print('You have picked up the ' + ask)
print(myPlayer.inventory)
# Exit Game command
if plaAction == 'exit':
myPlayer.gameOver = True
When the code gets to if ask in myPlayer.location.items[item.name]:
I get the following error. Looking up how to check a string in a list the results show that this should be possible.
For the line of code
if ask in myPlayer.location.items[item.name]:
the error in not in in
, the error is in here myPlayer.location.items[item.name]
This is because myPlayer.location.items
is a list and item.name
is a string. You can not use string as an index for list.
What you probably meant is this:
ask = input("What would you like to pick up?\n")
print(type(ask))
print(type(myPlayer.location.items))
for item in myPlayer.location.items:
if ask == item.name:
myPlayer.inventory.append(item)
print('You have picked up the ' + ask)
print(myPlayer.inventory)
Notice the line if ask == item.name:
, it checks if the user input is equal to some item name among the location's items. Then you do myPlayer.inventory.append(item)
to put the item into the inventory (instead of ask
, which is just a string).