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

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
Chris Freeman
Treehouse Moderator 59,597 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!!!