Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python Python Collections (2016, retired 2019) Dungeon Game Win or Lose

Hai Phan
Hai Phan
2,442 Points

I can't add new monsters, please help!

This is a part of my game that I'm testing out, I want to add a new monster after 3 moves, but I got this error "TypeError: object of type 'NoneType' has no len()". Anyone can help me, please!

import os
import random

SMALL_CELLS = [
(0,0),(1,0),(2,0),(3,0),(4,0),
(0,1),(1,1),(2,1),(3,1),(4,1),
(0,2),(1,2),(2,2),(3,2),(4,2),
(0,3),(1,3),(2,3),(3,3),(4,3),
(0,4),(1,4),(2,4),(3,4),(4,4)
]

ALL_MONSTERS = [(1,1), (2,2), (3,3)]
ALL_MOVES = ["A", "S", "D"]
custom_cells = SMALL_CELLS
player = random.choice(SMALL_CELLS)

def new_monster(custom_cells, player, ALL_MOVES, ALL_MONSTERS):
    if custom_cells == SMALL_CELLS:
        if len(ALL_MOVES) == 0:
            print("** New monster will appear in 3 moves **")
        elif len(ALL_MOVES) % 3 == 1:
            print("** New monster will appear in 2 moves **")
        elif len(ALL_MOVES) % 3 == 2:
            print("** New monster will appear in 1 moves **")
        elif len(ALL_MOVES) % 3 == 0:
            print("** New monster will appear in 3 moves **")
            abc = random.choice(custom_cells.copy().remove(player))
            ALL_MONSTERS.append(abc)
        return ALL_MONSTERS

print(new_monster(custom_cells, player, ALL_MOVES, ALL_MONSTERS))

1 Answer

The remove method doesn't return anything so in your code custom_cells.copy().remove(player) = none. I made the following modification:

elif len(ALL_MOVES) % 3 == 0:
    print("** New monster will appear in 3 moves **")

    abc = custom_cells.copy()
    abc.remove(player)
    abc = random.choice(abc)     
    ALL_MONSTERS.append(abc)
return ALL_MONSTERS

Not sure what this does without seeing the rest of the code.

Hai Phan
Hai Phan
2,442 Points

Thanks for the answer, this helps me a lot! :+1: