Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Below is the code for a text-based game. The code keeps giving me an error when I try to pick up an item the error

Below is the code for a text-based game. The code keeps giving me an error when I try to pick up an item the error isTraceback (most recent call last): File "/Users/parkermacpro1/PycharmProjects/TextBasedGame/main.py", line 60, in elif command.lower().split()[1] == 'get': IndexError: list index out of range

I need the game to store the item when the user says get "item name" and add to a list of all items picked up through all the rooms in the game.

Any help would be great!

the full code I have so far is as follows:

# data setup rooms = {'Bunk': {'name': 'Spaceship Bunk Room', 'West': 'Command', 'items': ['tablet'], 'text': 'You wake up and are all alone ' 'You can go North, South, East, West and Get items along your journey '}, 'Command': {'name': 'Spaceship Command Deck', 'South': 'Air', 'East': 'Bunk', 'text': 'You look out the window and in the distance of space you see a ' 'Space station with lights on.', 'items': ['4 digit pass code', 'digital code']}, 'Air': {'name': 'Spaceship Air Lock', 'East': 'Ship', 'North': 'Command', 'items': ['rocket fuel'], 'text': 'You look in the corner of the massive room and see a shuttle ship'}, 'Ship': {'name': 'Shuttle Ship', 'West': 'Air', 'South': 'Station', 'items': [], 'text': 'On your way to the station you see a planet through the window '}, 'City': {'name': 'Planet Abandoned City', 'South': 'Cave', 'North': 'Forest', 'West' 'items': ['gun'], 'text': 'You find your gun and have now collected all items ' 'The alien jumps out from a building and you shoot it. wining the game ' 'Enter Q to exit the game'}, 'Cave': {'name': 'Planet Cave', 'North': 'City', 'text': 'As you walk through cave you can see the city lights through the opening in the other side', 'items': ['flashlight']}, 'Forest': {'name': 'Planet Forest', 'West': 'Station', 'North': 'Cave', 'items': [], 'text': 'You land on the planet and see an alien running toward a city'}, 'Station': {'name': 'Shuttle Ship', 'East': 'Forest', 'North': 'Station', 'items': [], 'text': 'The light may be on but no one is on board the station'}} directions = ['North', 'South', 'East', 'West'] current_room = rooms['Bunk'] items = ['get'] # game loop while True: # display current location print() print('You are in {}.'.format(current_room['name'])) print(current_room['text']) # display objects if current_room['items']: print('In the room are: {}'.format(', '.join(current_room['items']))) # get user input command = input(' What do you do? ').strip() # movement if command in directions: if command in current_room: current_room = rooms[current_room[command]] else: # bad movement print("You can't go that way.") # quit game elif command.lower() in ('q', 'quit'): break # gather items elif command.lower().split()[0] == 'get': item = command.lower().split()[0] if item in current_room['items']: current_room['items'].remove(item) items.append(item) else: print("I don't see that here.") # bad command else: print("I don't understand that command."), 

I need the item to be picked up and saved until the final room. Thanks!!

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Financial management theory and practice

Authors: Eugene F. Brigham and Michael C. Ehrhardt

12th Edition

978-0030243998, 30243998, 324422695, 978-0324422696

Students also viewed these Programming questions