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 trialWilfredo Casas
6,174 PointsA couple of questions about the video
Hello guys, I have a couple of questions about the video that I was hoping you could help me with.
1.- (Video minute 1.35) In the function get_places (get_locations in the video), I don't really understand why he doesn't use the "else" after the "if". Also I don't understand why he writes "return get_locations" instead of just "get_locations()" to call the function. 2.- (Video minute 1.45) Instead of the "monster, door, player = get_locations()" before the "while True:", can't we just call the function inside the while? like this:
while True: get_locations() print("Welcome to the dungeon") ...
Thanks!
4 Answers
Jay McCormick
6,646 PointsGlad I could help, Wilfredo!
Think of the function get_locations() in this way: it is creating locations, but it's not telling anyone what those locations are. If you then execute the while-loop, it will find the monster, door, player variables empty.
It sounds like get_locations() is using variables called 'monster', 'door', and 'player' in its code block, and you're wondering why that doesn't solve the problem. the reason is that the while-loop can't look into the function to see what the function is doing. the only way to get the values out of the function and make them available to the while loop is to assign them to variables outside of the function itself by saying "a, b, c = function_name()".
Let me give you an example that you can try for yourself in a Python shell. create a variable called 'a' and set it to 'hello': "a = hello". now, define a function called other_a() that assigns the string "goodbye" to a variable named "a": def other_a(): a = "goodbye" return a. if you type 'a' into the shell, it will return 'hello', and when you run other_a() it will return "goodbye". the "a" variable you defined outside of the function and the "a" value you defined within the function are independent of each other. to get to the "a" variable in other_a(), you have to assign it to a variable outside of other_a(), just like you have to with get_locations().
Does that help?
Haider Ali
Python Development Techdegree Graduate 24,728 PointsHi there. At 1:35, the reason why Kenneth does not use an else clause is because it is much more efficient to just call the function again within itself. The conditional statement says that if either the monster, door or start are equal to each other, call this function again in order to make them all different. Once the function is called again, the same process will occur and it will continue to be called until the door, start and monster are in different locations. In programming, this is referred to as a 'recursive function' or 'currying'.
Also, the reason why Kenneth does not put monster, door, player = get_locations()
in the while loop is because if he did, then every turn the monster, start and door will be in a different position. This is why the positions are set only once outside of the while
loop.
Thanks, Haider
Steven Parker
231,269 PointsAs explained in the video, when the if condition is true, the return statement is executed which ends the function. So an explicit else is not needed, because everything following is only executed in the "else" situation.
The call to get_locations is used to set the start positions for the game, so it is done just once, before the loop. If it were done inside the loop, it would set the conditions to a new starting point for every move, making the game unplayable.
Wilfredo Casas
6,174 PointsThanks Steven, but what is instead of "monster, door, player = get_locations", he would just call (outside the loop) for the function "get_locations()"? Wouldn't that make more sense? I think I don't really understand why we are matching these 3 variables to the function, I had not seen that before.
Haider Ali
Python Development Techdegree Graduate 24,728 PointsHi Wilfredo, the get_locations
function returns 3 things: the position of the monster, door and player. If you have not learned about this already, this is how you would assign multiple variables on a single line. If i wanted to set a variable named num_1
to 1 and a variable named num_2
to 2, this is how I would do it in one line.
num_1, num_2 = 1, 2
Essentialy, what the monster, door, player = get_locations()
does is the following.
monster, door, player = monster_position, door_position, player_position
The get_locations
function returns the monster, door and player arguments in that order so you can assign all 3 arguments in one line. This may seem a little confusing at first, but if you jump into the python shell and play around for a couple of minutes you will understand it much better. Hope this has helped!
Jay McCormick
6,646 Pointshey Wilfredo - I understand your confusion. I don't recall when (or if) this comes up in the lesson: writing "monster, door, player = get_locations()" is creating something called a tuple. What's happening is get_locations() is returning three values. those values need to be stored somewhere so that you can do something with them, otherwise they just evaporate. the place they are stored is in the monster, door, and player variables. a longer way to do this would be to write a function for each variable, like "get_location_for_the_monster()" and assign the value that function creates to the monster variable, then write a function like "get_location_for_the_player()" and assign its return value to the player variable, etc. the simpler route is to create one function, get_locations(), that tells you the three locations you need (for the monster, the door, and the player), and then hold onto those locations by assigning them to variables called "monster", "door", and "player". all of that happens in that one line of code "monster, door, player = get_locations()".
hope that helps, and happy pythoning!
Wilfredo Casas
6,174 PointsThanks Jay, it does help. What is still a bit unclear to me is that, if I am already storing the "monster, door..." in variables inside the function, and then I am calling the before the "while", why would they evaporate? wouldn't they be stored in the moment I call the function? I think this confuses me because I had done some exercises with functions before but don't remember having a problem like that.
Wilfredo Casas
6,174 PointsWilfredo Casas
6,174 PointsYes! That example was superb, thank you!