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

Get_moves() function in dungeon_game

Hi guys, I would appreciate it if someone could answer me the following question. As I am new to programming, my understanding so far is, that the body within an if-statement is only executed iff the condition of the if-statement is true, isn't?

In the dugeon_game we define the cells constant as follows:

CELLS = [(0, 0), (0, 1), (0, 2),(1, 0), (1, 1), (1, 2),(2, 0), (2, 1), (2, 2)]

ā€¦ now we define the get_moves() function as follows:

***def get_moves(player): moves = ['LEFT', 'RIGHT', 'UP', 'DOWN']

if player[1] == 0: moves.remove('LEFT') if player[1] == 2: moves.remove('RIGHT') if player[0] == 0: moves.remove('DOWN') if player[0] == 2: moves.remove('UP') return moves***

So when we check if player[1] == 0 , we are actually checking the if (0,1) == 0 isn't like that? When will this be true?

Thanks for your help guys :-)

Cheers Urs

1 Answer

Fable Turas
Fable Turas
9,405 Points

Each CELLS unit is a tuple of x and y coordinates. As I recall, somewhere else in the game program there is a function that assigns 'player' to a random choice from the list of tuples in CELLS. So 'player' would equal (0, 1), or some other appropriate tuple from the CELLS list. To better understand when player[1] == 0 you might want to review the videos that talk about referencing members of a list or tuple by its index.
In an example where 'player' is equal to (1, 2), 'player[0]', or the member of 'player' at index 0, equals 1 and 'player[1]', or the member of 'player' at index 1, equals 2. So player[1] == 0 would be true any time 'player' equals (0, 0) or (1, 0) or (2, 0).