Question: Why will the following code print There's no such item in this room. Even when there IS an item in the room, as well as

Why will the following code print "There's no such item in this room." Even when there IS an item in the room, as well as not allowing the player to move at all?
Code:
def show_instructions():
# Print main menu and commands
print("Haunted House Adventure Game")
print("Collect 6 items to win the game, or be killed by the demon.")
print("Move commands: go South, go North, go East, go West")
print("Add to Inventory: get 'item name'")
def show_status(current_room, inventory, items_in_room):
# Display player's status
print("You are in the", current_room)
print("Inventory:", inventory)
if items_in_room:
print("You see the following item(s) in this room:", items_in_room)
def get_new_state(direction_from_user, current_room, rooms):
# Update player's status based on the given direction
if direction_from_user in rooms[current_room]:
return rooms[current_room][direction_from_user]
else:
print("You can't go that way.")
return current_room # Return the current room if the direction is not valid
def check_conditions(current_room, rooms, inventory):
# Check conditions for current room
items_in_room = rooms[current_room].get('item')
if current_room == 'Attic':
print("You have encountered the demon and have been killed!")
return False
elif items_in_room and not any(item in inventory for item in items_in_room.split(',')):
print("You see an item in this room. You can 'get' it.")
return True
def main():
show_instructions() # Show instructions before starting the game
# Define the rooms and items dictionary
rooms ={
'Kitchen': {'West': 'Dining Room', 'North': None, 'East': None, 'South': None, 'item': 'Salt'},
'Dining Room': {'South': 'Foyer', 'North': None, 'East': 'Kitchen', 'West': None, 'item': 'Lighter'},
'Foyer': {'West': 'Bathroom', 'East': 'Living Room', 'South': 'Library', 'North': 'Dining Room', 'item': None},
'Bathroom': {'East': 'Foyer', 'North': None, 'West': None, 'South': None, 'item': 'Bones'},
'Living Room': {'North': 'Attic', 'South': None, 'East': 'Attic', 'West': None, 'item': 'Cross'},
'Attic': {'South': 'Living Room', 'North': None, 'East': None, 'West': None, 'item': 'Demon'},
'Library': {'East': 'Mud Room', 'South': None, 'North': 'Foyer', 'West': None, 'item': 'Diary'},
'Mud Room': {'West': 'Library', 'North': None, 'East': None, 'South': None, 'item': 'Candle'},
}
# Initialize player's starting room, inventory
current_room = 'Kitchen'
inventory =[]
# Gameplay loop
while True:
# Display player's status
items_in_room = rooms[current_room].get('item')
show_status(current_room, inventory, items_in_room)
# Prompt the player for input
command = input("Enter your move: ").strip().lower()
# Parse the input command
if command.startswith('go '):
direction = command.split('')[1]
current_room = get_new_state(direction, current_room, rooms)
elif command.startswith('get '):
item = command.split('',1)[1]
if items_in_room and item in items_in_room.split(','):
inventory.append(item)
print("You have collected", item)
if len(inventory)==6 and current_room != 'Attic':
print("Congratulations! You have collected all items and defeated the demon!")
break
else:
print("There's no such item in this room.")
elif command == 'quit':
print("Quitting game.")
break
else:
print("Invalid command.")
# Check game conditions
if not check_conditions(current_room, rooms, inventory):
break
print("Thanks for playing the game. Hope you enjoyed it.")
# Run the main function
if __name__=="__main__":
main()

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!