I can exit and get items but I cannot figure out where my issue with directions. I am very new to python (first class) and this is our final project. Any guidance on which line of code is incorrect would be greatly appreciated!
rooms={
'Locker Room': {'go South':'Storage Room', 'go West': 'Coaches Office', 'go North': 'Teachers Lounge', 'go East': 'Concession Stand', 'Item': 'gym clothes'},
'Storage Room':{'go North':'Locker Room','go East':'Cafeteria','Item': 'basketballs'},
'Cafeteria':{'go West':'Storage Room', 'Item': 'sports drinks'},
'Concession Stand':{'go West': 'Locker Room', 'go North': 'Gym', 'Item': 'nachos'},
'Coaches Office': {'go East': 'Locker Room', 'Item': 'team films'},
'Teachers Lounge': {'go South': 'Locker Room', 'go East': 'Math Class','Item': 'referees'},
'Math Class': {'go West': 'Teachers Lounge', 'Item': 'teammates'},
'Gym':{'Item' : 'basketball team'}
}
directions: set[str] = {'go North', 'go South', 'go East', 'go West'} #valid moves
def instructions():
print('\n Welcome to the Battle of the Gym game')
print('The basketball team has taken over the gym, and your volleyball match is set to begin in one hour.')
print('You need to defeat the team before the opposing team arrives or forfeit your match.')
print('But before you go to battle you will need a few items.')
print('\n You must collect all 7 items to defeat the basketball team.')
print('\n Move commands: go North, go South, go West, go East')
print("\n Add to Inventory: get 'item name'")
print('\n Exit game: exit')
print('-------------------------------------------------------')
instructions() #prints the game title and how to play
#main loop
def main():
location = 'Locker Room' #starting place
inventory = [] #holds inventory
def show_status(): #where I am, what i have or don't have, or what I need
print('')
print(f'You are currently in the {location}')
print(f'You currently have: ', *inventory, sep=' ')
if rooms[location]['Item'] in inventory:
print('This room is empty.')
else:
print(f'You see ' + rooms[location]['Item'])
while True:
show_status() #prints current room and inventory
possible_moves = rooms.get(location, {}) #looks for valid moves
current_item = rooms[location]['Item'] #lets know item
command = input('\nPlease enter a direction or take the available item: \n').strip().lower() #ask for users move
if command in directions: #check for valid move
if command not in rooms[location]: #if not valid command
print("You can't go that way. Enter a new direction.")
elif command in possible_moves: #valid move
location = possible_moves.get(command, location)
if location == 'Gym': #checks to see if game over
if len(inventory) != 7: #if inventory less than 7 game over
print("You've been beat! Game Over!")
break
else: #if inventory is 7 you won
print('Congratulations! You have collected all the items and defeated the basketball team!')
break
elif command == 'exit': #exits game
print("Thank you for playing. Hope you enjoyed the game.")
break
elif command == 'get {}'.format(current_item): #add item to inventory
if current_item not in inventory: #checks inventory and if not in iventory retrieves item
print('You got ' + current_item,'in your inventory.')
inventory.append(current_item) #adds to inventory list
elif current_item in inventory: #already in inventory list tells user that
print('You already have this item.')
else: #no items available to end loop
print('No items available.')
else: #user enters anything not in directions
print('Invalid input.')
if __name__ =='__main__':
main()
I tried changing rooms dictionary to go North and then directions to just north to match dictionary after the first set didn't work.
I've identified the issue in your code and provided a corrected version:
The Problem:
The issue lies in how you're checking for valid directions in the if command in rooms[location] line. This line only checks if the direction exists as a key in the current room's dictionary, not if the corresponding value points to another room.
Code to the Solution:
Explanation:
1. Filtering Valid Moves: The
possible_movesdictionary is created by iterating through the current room's items and filtering for keys that are also present in thedirectionsset. This ensures you only consider valid directions from the current room.2. Checking Valid Moves: The if command not in
possible_movescheck now verifies if the entered direction is actually a valid move from the current room, preventing invalid movements.With this fix, your game should correctly handle user input for directions and allow players to navigate through the rooms as intended.