Movatterモバイル変換


[0]ホーム

URL:


Python for Beginners2015.11.01

User-defined functions

Introduction

Programmers can deal with some pretty complex and abstract problems, but onesign of a good programmer is that they’re lazy. They only like to deal with onething at a time. So you need a way to break up problems into smaller, discretepieces, which lets you focus on just the piece you want to.

Functions are one way to do this abstraction in Python. Let’s taketurtle.reset() for example.reset is a function we call on ourturtle, andit is actually an abstraction for a number of steps, namely:

  • Erase the drawing board.
  • Set the width and color back to default.
  • Move the turtle back to its initial position.

But because all the code is contained in the function, we don’t have to worry about thesedetails. We can simplycall this function, and know it will do what it says for us.

So - how to write your own?

A function can be defined with thedef keyword in Python:

defline_without_moving():turtle.forward(50)turtle.backward(50)

This function we defined is calledline_without_moving and it isan abstraction for two turtle steps - a move forward and a movebackward.

To use it (or as it is usually called, “to call it”), write its namefollowed by parentheses:

line_without_moving()turtle.right(90)line_without_moving()turtle.right(90)line_without_moving()turtle.right(90)line_without_moving()

We could write more functions to remove some of the repetition:

defstar_arm():line_without_moving()turtle.right(360/5)for_inrange(5):star_arm()

Important

Python usesindenting with whitespace to identify blocks of codethat belong together. In Python a block (like the functiondefinitions shown above) is introduced with a colon at the end of theline and subsequent commands are indented — usually 4 spacesfurther in. The block ends with the first line that isn’t indented.

This is different to many other programming languages, which usespecial characters (like curly braces{}) to group blocks ofcode together.

Never use tab characters to indent your blocks, only spaces. Youcan – and should – configure your editor to put 4 spaces when youpress the tab key. The problem with using tab characters is thatother python programmers use spaces, and if both are used in thesame file python will read it wrong (in the best place, it willcomplain, and in the worst case, weird, hard to debug bugs willhappen).

A function for a square

Exercise

Write a function that draws a square. Could you use this function to improve thetilted squares program? If you change the program to use a function, is it easierto experiment with?

Solution

deftilted_square():turtle.left(20)# now we can change the angle only herefor_inrange(4):turtle.forward(50)turtle.left(90)tilted_square()tilted_square()tilted_square()# bonus: you could have a separate function for drawing a square,# which might be useful later:defsquare():for_inrange(4):turtle.forward(50)turtle.left(90)deftilted_square():turtle.left(20)square()# etc

A function for a hexagon

Exercise

Write a function that draws a hexagon.

_images/hexagon.png

Now combine that function into a honeycomb. Just make it with a single layer like this:

_images/honeycomb.png

Give it a good go!

Hint

Make sure your hexagon function returns your turtle to exactly the sameposition and angle it was before it was asked to draw the hexagon. Thismakes it easier to reason about.

Solution

defhexagon():for_inrange(6):turtle.forward(100)turtle.left(60)for_inrange(6):hexagon()turtle.forward(100)turtle.right(60)

You could also put theturtle.forward(100);turtle.right(60) portion in thefunction, but you better not call ithexagon in that case. That’smisleading because it actually draws a hexagon and then advances to a positionwhere another hexagon would make sense in order to draw a honeycomb. If youever wanted to reuse your hexagon function outside of honeycombs, that would beconfusing.


[8]ページ先頭

©2009-2025 Movatter.jp