Night 11: Teach your pet a trick
이미 Python 또는 의사 코드로 이날의 학습을 마쳤습니다. 두 언어 모두에서 완료한 것으로 인정됩니다. 원하면 다시 연습하세요.
이 페이지는 최신 브라우저(SharedArrayBuffer 지원)가 필요합니다. Chrome, Edge, Firefox 또는 Safari를 최신 버전으로 업데이트해 주세요.
English
Tonight: teach your pet a trick
- Your pet can learn tricks! You teach a trick once, then use it again and again.
- You will learn one thing: functions.
Words for tonight
| Word | Meaning |
|---|---|
| function | a named block of code; a trick with a name |
| define | say what something is; teach the trick |
| call | use the function; ask for the trick |
English
Define a function
defmeans define. Then the name, brackets()and a colon:.- The indented lines are the trick.
def wave():
print("*waves*")
print("Hello!")
- Press Run. Nothing happens! You taught the trick, but you did not ask for it.
English
Call the function
- To use the trick, write its name with brackets:
wave().
def wave():
print("*waves*")
wave()
wave()
- Each call runs all the indented lines of the function.
English
Why use functions?
| Without a function | With a function |
|---|---|
| copy the same lines again and again | write them once |
| a change means changing every copy | change one place |
| long code that is hard to read | short names like wave() |
English
Functions can call functions
- A big trick can be made from small tricks.
def wave():
print("*waves*")
def jump():
print("*jumps*")
def party():
wave()
jump()
print("Party time!")
party()
- Define a function before you call it.
- Now teach your pet some tricks in the tasks!
난이도 2/5 · 140코인 + 오늘 첫 완료 시 작은 보너스 추가
Teach your pet its first trick. Define a function wave() that prints *waves*. Then call it two times.
출력을 보려면 '실행'을 클릭하세요.
난이도 2/5 · 140코인 + 오늘 첫 완료 시 작은 보너스 추가
Define two more tricks: sit() prints *sits* 및 spin() prints *spins*. Then call sit(), spin() 및 sit() again.
출력을 보려면 '실행'을 클릭하세요.
난이도 3/5 · 200코인 + 오늘 첫 완료 시 작은 보너스 추가
Define show(). It calls wave(), then sit(), then prints Ta-da!. Call show() once.
출력을 보려면 '실행'을 클릭하세요.
난이도 3/5 · 200코인 + 오늘 첫 완료 시 작은 보너스 추가
Define bedtime(). It uses a for loop to print Zzz three times. Call it once.
출력을 보려면 '실행'을 클릭하세요.