Python Development Course
Chapter
>
Level
Creating your own Functions
Using Conditions in Functions
Objective
Open gates and repair bridges using a single custom function by using conditions.
The path across the swamp has a few obstacles that you need to overcome, some of these obstacles are the same and you can automate traversing them by using a single custom function with conditions.
There are two types of obstacles in your way, doors you need to open and close and bridges you need to repair. Create a custom function that can go through both by setting it to take the commands "open" and "build" as arguments for the function.
def navigate_swamp(action):
if action == "build":
# Add code to build bridge and move forward
# Make sure code is repeated 3 times
if action == "open":
# Add code to open door and move forward
# Then code to turn around and close the door
Inside the custom function, under the build condition, use the build() function in order to repair the collapsed bridge in the middle of the map, like this player.build("bridge"). Set it so the player can move forward and build the bridge three (3) times so you can get across.
For the second option in the custom function used to open and close gates, use the open() and close() functions. Set it so you open a door, move forward then turn around and close the door.
Walk to the dark X marks inf front of doors and use the custom function with the open command to get through gates, like this navigate_swamp("open") . Also collect all the logs in the field and make your way to the first light X mark you encounter, use the custom function with the build command to get across the stream, like this: navigate_swamp("build").
Reach the exit marked by the star at the end of the path using the custom function in order to complete the level.