I'm trying to do a text game for school and having issues adding the items to inventory, I have the moving between rooms working but the inventory is what's getting me now. I just keep getting errors no matter how I set up the get items, I have issues. Is there an easy way to add this functionality that I am missing?
# Game Description
# The main character is an ice hockey goalie for the local home team.
# Their cities rival is playing them tonight and the opposing team’s goalie broke in to the locker room
# and hid the home teams goalie equipment all over the building to try and rattle him causing him to lose time warming up.
# This includes his helmet, chest armor, blocker, glove, and each leg pad.
# Time is clicking off and the home team goalie needs to find his stuff so he can get dressed and warm up.
rooms = {
'Front Lobby': {'East': 'Ice Rink', 'Item': 'Glove'},
'Ice Rink': {'North': 'Away Locker Room', 'West': 'Front Lobby', 'East': 'Mens Locker Room', 'South': 'Lounge', 'Item': 'Helmet'},
'Away Locker Room': {'East': 'Pro Shop', 'South': 'Ice Rink'},
'Pro Shop': {'West': 'Away Locker Room', 'Item': 'Villian'},
'Mens Locker Room': {'West': 'Ice Rink', 'Item': 'Leg Pad 2'},
'Lounge': {'West': 'Womens Locker Room', 'East': 'Skate Rentals', 'North': 'Ice Rink', 'Item': 'Blocker'},
'Womens Locker Room': {'East': 'Lounge', 'Item': 'Leg Pad 1'},
'Skate Rentals': {'West': 'Lounge', 'Item': 'Chest Armor'}
}
gameOn = True
inventory = []
#moves player from room to room
def move(player, direction):
global rooms
current_room = player
# check if there is a room in the specified direction
if direction in rooms[current_room]:
current_room = rooms[current_room][direction]
# error handling
else:
print("There is nothing in that direction!")
return player # Returning the same room if nothing Exists in the direction
# return the player state
return current_room # Indent Error
#displays rules at start of game
def showRules():
print("- Collect 6 items to win the game, or have to get clowned by the other goalie.\n" "Move commands: go South, go North, go East, go West.\n" "Add to Inventory: get 'item name'. Once all items collected type: ‘Finish’")
def main():
showRules()
player = "Front Lobby"
while gameOn:
current_room = player
# output
print(f"\nYou are in the {current_room}")
print('Inventory: ', inventory)
# Goalie got you
if player == 'Pro Shop':
print('You make more excuses than saves. GAME OVER')
break
# input validation and parsing
print("----------------")
player_move = input("Enter your move:\n")
# invalid move syntax
if 'go' in player_move or 'Finish' in player_move:
# split string
action = player_move.split(' ')
print(action) # move
if action[0] == 'go':
player = move(player, action[1]) # Assigning the value to player
elif action == 'Finish':
if len(inventory) == 6:
print('Awesome! Now get on the ice! You Win.')
else:
print('Keep collecting items')
# invalid action
else:
print("Invalid command!")
elif 'get' in player_move:
action = player_move.split(' ')
getItem(current_room, action[1])
else:
print('Invalid Command')
continue
def getItem(player, item):
current_room = player
if item in rooms[current_room]:
inventory.append(item)
print(inventory)
main()
updated the code 11:05 EST
Try to write your getItem function more like this:
def getItem(current_room, item):
if item == rooms[current_room]["Item"]:
inventory.append(item)
print(inventory)
Explanation: in you original expression item in rooms[current_room]
, rooms[current_room] points to a dictionary, so the x in dict
expression will check if x
is part of the key of that dictionary. But if I understand well, you are only interested by the "Item" part of that dict, and you have to make the comparison with the value and not with the key.
I ran it through my Python compiler and came up with a syntax error. You missed the second condition and action in your block in one of the last if/elif/else blocks in main()
. I don't know much but I think the code might be this:
elif 'get' in player_move:
action = player_move.split(' ')
if action[1] == <second condition here>: #The second condition was missed here
<action> #The action was missed here
IDK about the code you want there.
I'm not completely sure but I think there is something wrong with the move()
function. Try the other answer, adding this snippet to getItem()
:
if item == rooms[current_room]["Item"]:
inventory.append(item)
print(inventory)
If that doesn't work, then send me an email: shivernscorch@gmail.com and we can take it from there.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.