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 showinstructions:
# Print main menu and commands
printHaunted House Adventure Game"
printCollect items to win the game, or be killed by the demon."
printMove commands: go South, go North, go East, go West"
printAdd to Inventory: get 'item name'"
def showstatuscurrentroom, inventory, itemsinroom:
# Display player's status
printYou are in the", currentroom
printInventory: inventory
if itemsinroom:
printYou see the following items in this room:", itemsinroom
def getnewstatedirectionfromuser, currentroom, rooms:
# Update player's status based on the given direction
if directionfromuser in roomscurrentroom:
return roomscurrentroomdirectionfromuser
else:
printYou can't go that way."
return currentroom # Return the current room if the direction is not valid
def checkconditionscurrentroom, rooms, inventory:
# Check conditions for current room
itemsinroom roomscurrentroomgetitem
if currentroom 'Attic':
printYou have encountered the demon and have been killed!"
return False
elif itemsinroom and not anyitem in inventory for item in itemsinroom.split:
printYou see an item in this room. You can 'get' it
return True
def main:
showinstructions # 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
currentroom 'Kitchen'
inventory
# Gameplay loop
while True:
# Display player's status
itemsinroom roomscurrentroomgetitem
showstatuscurrentroom, inventory, itemsinroom
# Prompt the player for input
command inputEnter your move: striplower
# Parse the input command
if command.startswithgo :
direction command.split
currentroom getnewstatedirection currentroom, rooms
elif command.startswithget :
item command.split
if itemsinroom and item in itemsinroom.split:
inventory.appenditem
printYou have collected", item
if leninventory and currentroom 'Attic':
printCongratulations You have collected all items and defeated the demon!"
break
else:
printTheres no such item in this room."
elif command 'quit':
printQuitting game."
break
else:
printInvalid command."
# Check game conditions
if not checkconditionscurrentroom, rooms, inventory:
break
printThanks for playing the game. Hope you enjoyed it
# Run the main function
if namemain:
main
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
