Programmers can deal with some pretty complex and abstract problems, but one sign of a good programmer is that they’re lazy. They only like to deal with one thing at a time. So you need a way to break up problems into smaller, discrete pieces, which lets you focus on just the piece you want to.
Functions are one way to do this abstraction in Python. Let’s take
turtle.reset()
for example. reset
is a function we call on our turtle
, and
it is actually an abstraction for a number of steps, namely:
그림판을 지우세요.
But because all the code is contained in the function, we don’t have to worry about these details. We can simply call this function, and know it will do what it says for us.
그래서 함수를 어떻게 작성하실건가요?
A function can be defined with the def
keyword in Python:
def line_without_moving():
turtle.forward(50)
turtle.backward(50)
This function we defined is called line_without_moving
and it is
an abstraction for two turtle steps - a move forward and a move
backward.
To use it (or as it is usually called, “to call it”), write its name followed 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:
def star_arm():
line_without_moving()
turtle.right(360 / 5)
for _ in range(5):
star_arm()
중요
Python uses indenting with whitespace to identify blocks of code that belong together. In Python a block (like the function definitions shown above) is introduced with a colon at the end of the line and subsequent commands are indented — usually 4 spaces further in. The block ends with the first line that isn’t indented.
This is different to many other programming languages, which use
special characters (like curly braces {}
) to group blocks of
code together.
Never use tab characters to indent your blocks, only spaces. You can – and should – configure your editor to put 4 spaces when you press the tab key. The problem with using tab characters is that other python programmers use spaces, and if both are used in the same file python will read it wrong (in the best place, it will complain, and in the worst case, weird, hard to debug bugs will happen).
사각형을 그리는 함수를 작성해보세요. 사각형을 기울이는 프로그램에 이 함수를 사용할 수 있을까요? 함수를 사용하는 프로그램을 바꾸면 쉽게 실험할 수 있을까요?
def tilted_square():
turtle.left(20) # now we can change the angle only here
for _ in range(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:
def square():
for _ in range(4):
turtle.forward(50)
turtle.left(90)
def tilted_square():
turtle.left(20)
square()
# etc
육각형을 그리는 함수를 작성하세요.
이제 이 함수를 벌집에 합쳐보세요. 다음과 같이 단일 층으로 만드세요.
Give it a good go!
힌트
Make sure your hexagon function returns your turtle to exactly the same position and angle it was before it was asked to draw the hexagon. This makes it easier to reason about.
def hexagon():
for _ in range(6):
turtle.forward(100)
turtle.left(60)
for _ in range (6):
hexagon()
turtle.forward(100)
turtle.right(60)
You could also put the turtle.forward(100); turtle.right(60)
portion in the
function, but you better not call it hexagon
in that case. That’s
misleading because it actually draws a hexagon and then advances to a position
where another hexagon would make sense in order to draw a honeycomb. If you
ever wanted to reuse your hexagon function outside of honeycombs, that would be
confusing.