Cori Sparks
Cori Sparks

Reputation: 43

NoneType Object has no attribute 'get' for my text adventure

I have no idea what is wrong with this. I have been starring at it too long and need help finding the problem.

def get_player_command():
"""the player input"""
return raw_input('Action: ').lower().strip()

def play():
    print("++++++++++++++++++++++++++++++++++")
    print("|      DEATHSCHOOL!!!!           |")
    print("|                                |")
    print("++++++++++++++++++++++++++++++++++\n\n\n")
    print("You walk up the stairs on the first day of school.\n Something doesn't feel right......\n The parking lot was full, but the building is eerily silent.\n")
player = Player()
while True:
    room = world.tile_at(player.x, player.y)
    print(room.intro_text())
    room.modify_player(player)
    choose_action(room, player)



def action_adder(action_dict, hotkey, action, name):

    action_dict[hotkey.lower()] = action

    action_dict[hotkey.upper()] = action
    print("{}: {}".format(hotkey, name))




def get_available_actions(room,player):
    actions = collections.OrderedDict()
    print ("Choose an action: ")
    if player.backpack:
        action_adder(actions, 'b', player.print_pack, "Print Backpack")
    if isinstance (room, world.EnemyTile) and room.enemy.is_alive():
        action_adder(actions, 'f', player.attack, "Fight!")
    else:
        if world.tile_at(room.x, room.y - 1):
            action_adder(actions, 'w', player.move_forward, "Go Forward!")
        if world.tile_at(room.x, room.y + 1):
            action_adder(actions, 's', player.move_backward, "Go Backward!")
        if world.tile_at(room.x + 1, room.y):
            action_adder(actions, 'd', player.move_right, "Go Right!")
        if world.tile_at(room.x - 1, room.y):
            action_adder(actions, 'a', player.move_left, "Go Left!")

    if player.lifepoints < 100:
        action_adder(actions, 'h', player.heal, "Heal")

        return actions

def choose_action(room, player):
    action = None

    while not action:
        available_actions = get_available_actions(room, player)

        action_input = raw_input("Action: ").lower().strip()
        action = available_actions.get(action_input)
        if action:
            action()

    else:
        print("Invalid action!")

play()

here is the traceback

Traceback (most recent call last):
  File "E:\CoriSparks_Portfolio\DeathSchool\action.py", line 110, in <module>
    play()
  File "E:\CoriSparks_Portfolio\DeathSchool\action.py", line 60, in play
    choose_action(room, player)
  File "E:\CoriSparks_Portfolio\DeathSchool\action.py", line 103, in choose_action
    action = available_actions.get(action_input)
AttributeError: 'NoneType' object has no attribute 'get'

Last night it was letting me use the w, s and b hotkeys, and was only giving me the error when I used f to fight an enemy. Now it is giving me the error no matter what I do. Also, it is only printing 'w' and 's' at the beginning not' w' 's', 'a' and 'd' enter image description here

Upvotes: 0

Views: 318

Answers (1)

TemporalWolf
TemporalWolf

Reputation: 7952

available_actions is None in this case, which means get_available_actions(room, player) is returning None.

this is probably because

if player.lifepoints < 100:
        action_adder(actions, 'h', player.heal, "Heal")

        return actions

it only returns something if this is true.

TL;DR:

your return actions is improperly indented.


How I figured that out:

action = available_actions.get(action_input)

AttributeError: 'NoneType' object has no attribute'get'

this shows me that what is attached to the get is what is None: available_actions.

available_actions = get_available_actions(room, player)

assigns it from the output of get_available_actions(), which means that function is returning None.

Upvotes: 2

Related Questions