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 trial

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

Carlos Marin
Carlos Marin
8,009 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 68,441 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!!!