Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialHai Phan
2,442 PointsI 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
KRIS NIKOLAISEN
54,971 PointsThe 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
2,442 PointsHai Phan
2,442 PointsThanks for the answer, this helps me a lot!