2016-12-09 7 views
0

私は何が間違っているのか分かりません。私はあまりにも長くそれを主演しており、問題を見つけるのを助ける必要があります。ここNoneTypeオブジェクトには、テキスト冒険のための属性 'get'がありません

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() 

は、それは私がW、SおよびBホットキーを使用せた、とだけ私が敵と戦うために、Fを使用し、エラーを与えていたトレースバック

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' 

最後の夜です。今は私が何をしてもエラーが出ます。また、それだけで 'と 『D』 enter image description here

答えて

2

available_actionsget_available_actions(room, player)を意味し、この場合はなし、なしを返していないです'、「W」のない初めには「w」と「s」が印刷されます。これが本当であるならば、それは唯一のものを返し

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

     return actions 

ので

これはおそらくです。

TL; DR:

あなたreturn actionsが正しくインデントされます。

私はそれを考え出した方法

action = available_actions.

'を取得する' これは何を得るために装着されていることNoneあるものであることを私に示して(action_input)

AttributeError: 'NoneType' object has no attributeを取得:available_actions

available_actions = get_available_actions(room, player)

関数Noneを返していることを意味する、get_available_actions()の出力からそれを割り当てます。

+1

NoneTypeオブジェクトの99%は属性がありません...誤って返された関数からエラーが発生しました。 – Gribouillis

+0

これは単純なものでなければならないことを知っていました。ありがとうございました。すべてのアクションキーを印刷していない理由を教えてください。 –

+0

私はそれを完全に理解した。ありがとう、ありがとう、ありがとう:) –

関連する問題