Python Python Collections (2016, retired 2019) Dungeon Game Movement

Carlos Marin
Carlos Marin
Python Web Development Techdegree Student 5,491 Points

What is the purpose of the get_locations() function?

def get_locations(): 
    return random.sample(CELLS, 3)

I am wondering why Kenneth is putting this function into another function? Does it matter? is it preference or style? why don't we put

player, monster, door = random.sample(CELLS, 3) 

in the world scope?

1 Answer

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 58,878 Points

Good question! Both styles work equally well.

By using a function call, it may allow the main loop of the program to more easily read, in that, you don’t have to immediately understand how the locations are derived. It also allows changing the code for picking positions without disturbing the main loop. For example if you wanted to restrict the cells returned to be a minimum distance apart.

On the other hand, if the position code is a simple one line expression, in-line may work just as well.

Post back if you have more questions. Good luck!!!