Python Development Course
Chapter
>
Level

Creating your own Functions
Creating a function

While there are a whole bunch of functions already available to you in order to perform all sorts of actions, you can expand the actions you can perform by creating and using custom functions. These allow you to reuse the same block of code in multiple areas of your program with a single line of code.

Custom functions allow you to simplify your code and and cut down the amount of code you need to write down. Using a custom function, push the rocks in this field and reach the exit using no more than 22 lines of code.

Guide

Objective

Push rocks to reach the exit writing no more than twenty two (22) lines of code.

There are several ways to save time coding, sometimes you’ll want to reuse the same strip of code multiple times. In previous chapters we introduced loops and lists to make code more efficient, another way to make code more efficient is creating custom functions.

def repeat_push(): player.push() player.move_forward() player.push() player.move_forward() player.push() player.move_forward() player.push() player.move_forward()

The above code is a custom function which will run the tabbed code, with this the player will push an object and move forward four (4) times.

Use this custom function, to move the rocks in the field and reach the exit. Do this while writing no more than twenty two (22) lines of code.

Code book