Home > Net >  Unable to Move Room to Room in text-based game for IT Python Class
Unable to Move Room to Room in text-based game for IT Python Class

Time:10-14

''' I've been working on this for about two weeks now, I can't seem to figure out the logic for the code to properly work and I'm having trouble understanding how to properly pull a key or value from a dictionary. Please note I'm a beginner and some of the concepts just go over my head. '''

''' Below is my code and one of the many errors I have received. Any guidance would be appreciated. '''

rooms = {
     'Camp Crystal Lake Office': {'South': 'Packanack Lodge', 'East': 'Creepy Forest',
                                 'North': 'Main Entrance', 'West': 'Mess Hall'},
    'Main Entrance': {'South': 'Camp Crystal Lake Office', 'East': 'Abandoned Shack'},
    'Abandoned Shack': {'Jason Voorhees'},
    'Mess Hall': {'East': 'Camp Crystal Lake Office'},
    'Creepy Forest': {'North': 'Higgins Haven', 'West': 'Camp Crystal Lake Office'},
    'Higgins Haven': {'South': 'Creepy Forest'},
    'Packanack Lodge': {'East': "Camping Area", 'North': 'Camp Crystal Lake Office'},
    'Camping Area': {'West': 'Packanack Lodge'}
}
items_needed = {
    'Main Entrance': 'rifle',
    'Mess Hall': 'Flash Light',
    'Creepy Forest': 'Battery',
    'Higgins Haven': 'Car',
    'Packanack Lodge': 'Running Sneakers',
    'Camping Area': 'Bear Trap'
}

    def game_play_rules():
        print('Welcome to Camp Crystal Lake!!')
        print('You are able to move North, South, East, West, but Jason Voorhees is lurking around.')
        print("You will need to collect all 6 items to escape Jason's wrath")
        print("If you want to leave the camp immediately, type Exit otherwise enjoy the game!")

# create function to change the location


def get_new_location(location, direction):
    new_location = location
    for i in rooms:
        if i == location:
            if direction in rooms[i]:
                new_location = rooms[i][direction]
    return new_location

game_play_rules()
location = 'Camp Crystal Lake Office'
inventory = []

while True:
    print('\nYou are currently at the ', location) # shows player where they are starting
    if location == 'Abandoned Shack':
        print("You walked into Jason's shack, better luck next summer.", end= '')
        for i in range(100):
            for j in range(1000000):
                pass
            print('.', end=' ', flush = True)
        print()
        if len(inventory) == 6:
            print("You collected all items and were able to escape - enjoy the rest of your summer")
        else:
            print("Welp, you failed to collect all the items, game over")
        break

    direction = input('Which way do you want to go: -->' )
    print('You want to go ', (get_new_location(location, direction)))
    print(f'\nThis location has ',(location, items_needed))
    print('Your inventory has ', inventory)
    print('\nYou already have that in your inventory', inventory)

    if direction.lower() == [location][items_needed].lower():
        if items_needed[location] not in inventory:
            inventory.append(items[location])
        continue
    direction = direction.capitalize()
    if direction == 'Exit':
        exit(0)
    if direction == 'North' or direction == 'South' or direction == 'East' or direction == 'West':
        new_location = get_new_location((location, direction))
        if new_location == location:
            print('I would not wander to far if I were you.')
        else:
            location = new_location
    else:
        print('Wrong way!!')

''' Error: Welcome to Camp Crystal Lake!! You are able to move North, South, East, West, but Jason Voorhees is lurking around. You will need to collect all 6 items to escape Jason's wrath If you want to leave the camp immediately, type Exit otherwise enjoy the game!

You are currently at the Camp Crystal Lake Office Which way do you want to go: -->south You want to go Camp Crystal Lake Office Traceback (most recent call last): File "C:\Users\jr0sa_000\PycharmProjects\pythonMilestone_v1\Project 2.py", line 66, in if direction.lower() == [location][items_needed].lower(): TypeError: list indices must be integers or slices, not dict

This location has ('Camp Crystal Lake Office', {'Main Entrance': 'rifle', 'Mess Hall': 'Flash Light', 'Creepy Forest': 'Battery', 'Higgins Haven': 'Car', 'Packanack Lodge': 'Running Sneakers', 'Camping Area': 'Bear Trap'}) Your inventory has []

You already have that in your inventory [] # '''

CodePudding user response:

I think you meant direction == rooms[location] instead of if direction.lower() == [location][items_needed].lower():

rooms = {
     'Camp Crystal Lake Office': {'South': 'Packanack Lodge', 'East': 'Creepy Forest',
                                 'North': 'Main Entrance', 'West': 'Mess Hall'},
    'Main Entrance': {'South': 'Camp Crystal Lake Office', 'East': 'Abandoned Shack'},
    'Abandoned Shack': {'Jason Voorhees'},
    'Mess Hall': {'East': 'Camp Crystal Lake Office'},
    'Creepy Forest': {'North': 'Higgins Haven', 'West': 'Camp Crystal Lake Office'},
    'Higgins Haven': {'South': 'Creepy Forest'},
    'Packanack Lodge': {'East': "Camping Area", 'North': 'Camp Crystal Lake Office'},
    'Camping Area': {'West': 'Packanack Lodge'}
}
items_needed = {
    'Main Entrance': 'rifle',
    'Mess Hall': 'Flash Light',
    'Creepy Forest': 'Battery',
    'Higgins Haven': 'Car',
    'Packanack Lodge': 'Running Sneakers',
    'Camping Area': 'Bear Trap'
}

def game_play_rules():
    print('Welcome to Camp Crystal Lake!!')
    print('You are able to move North, South, East, West, but Jason Voorhees is lurking around.')
    print("You will need to collect all 6 items to escape Jason's wrath")
    print("If you want to leave the camp immediately, type Exit otherwise enjoy the game!")

# create function to change the location


def get_new_location(location, direction):
    new_location = location
    for i in rooms:
        if i == location and direction in rooms[i]:
            new_location = rooms[i][direction]
    return new_location

game_play_rules()
location = 'Camp Crystal Lake Office'
inventory = []

while True:
    print('\nYou are currently at the ', location) # shows player where they are starting
    if location == 'Abandoned Shack':
        print("You walked into Jason's shack, better luck next summer.", end= '')
        for _ in range(100):
            print('.', end=' ', flush = True)
        print()
        if len(inventory) == 6:
            print("You collected all items and were able to escape - enjoy the rest of your summer")
        else:
            print("Welp, you failed to collect all the items, game over")
        break

    direction = input('Which way do you want to go: -->' ).lower()
    print('You want to go ', (get_new_location(location, direction)))
    print(f'\nThis location has ',(location, items_needed))
    print('Your inventory has ', inventory)
    print('\nYou already have that in your inventory', inventory)

    if direction == rooms[location]:
        if items_needed[location] not in inventory:
            inventory.append(items_needed[location])
        continue
    direction = direction.capitalize()
    if direction == 'Exit':
        exit(0)
    if direction in ['North', 'South', 'East', 'West']:
        new_location = get_new_location((location, direction))
        if new_location == location:
            print('I would not wander to far if I were you.')
        else:
            location = new_location
    else:
        print('Wrong way!!')

CodePudding user response:

new_location = get_new_location((location, direction))

should be

new_location = get_new_location(location, direction)

Also fixed issue with pointer being incorrect.

rooms = {
     'Camp Crystal Lake Office': {'South': 'Packanack Lodge', 'East': 'Creepy Forest',
                                 'North': 'Main Entrance', 'West': 'Mess Hall'},
    'Main Entrance': {'South': 'Camp Crystal Lake Office', 'East': 'Abandoned Shack'},
    'Abandoned Shack': {'Jason Voorhees'},
    'Mess Hall': {'East': 'Camp Crystal Lake Office'},
    'Creepy Forest': {'North': 'Higgins Haven', 'West': 'Camp Crystal Lake Office'},
    'Higgins Haven': {'South': 'Creepy Forest'},
    'Packanack Lodge': {'East': "Camping Area", 'North': 'Camp Crystal Lake Office'},
    'Camping Area': {'West': 'Packanack Lodge'}
}
items_needed = {
    'Main Entrance': 'rifle',
    'Mess Hall': 'Flash Light',
    'Creepy Forest': 'Battery',
    'Higgins Haven': 'Car',
    'Packanack Lodge': 'Running Sneakers',
    'Camping Area': 'Bear Trap'
}

def game_play_rules():
    print('Welcome to Camp Crystal Lake!!')
    print('You are able to move North, South, East, West, but Jason Voorhees is lurking around.')
    print("You will need to collect all 6 items to escape Jason's wrath")
    print("If you want to leave the camp immediately, type Exit otherwise enjoy the game!")

# create function to change the location


def get_new_location(location, direction):
    new_location = location
    for i in rooms:
        if i == location and direction in rooms[i]:
            new_location = rooms[i][direction]
    return new_location

game_play_rules()
location = 'Camp Crystal Lake Office'
inventory = []

while True:
    print('\nYou are currently at the ', location) # shows player where they are starting
    if location == 'Abandoned Shack':
        print("You walked into Jason's shack, better luck next summer.", end= '')
        for _ in range(100):
            print('.', end=' ', flush = True)
        print()
        if len(inventory) == 6:
            print("You collected all items and were able to escape - enjoy the rest of your summer")
        else:
            print("Welp, you failed to collect all the items, game over")
        break

    direction = input('Which way do you want to go: -->' ).lower()
    print('You want to go ', (get_new_location(location, direction)))
    print(f'\nThis location has ',(location, items_needed))
    print('Your inventory has ', inventory)
    print('\nYou already have that in your inventory', inventory)

    if direction == rooms[location]:
        if items_needed[location] not in inventory:
            inventory.append(items_needed[location])
        continue
    direction = direction.capitalize()
    if direction == 'Exit':
        exit(0)
    if direction in ['North', 'South', 'East', 'West']:
        new_location = get_new_location(location, direction)
        if new_location == location:
            print('I would not wander to far if I were you.')
        else:
            location = new_location
    else:
        print('Wrong way!!')
  • Related