01. 파이썬 함수 정의 및 호출
print("Hello")
num_char = len("Hello")
print(num_char)
def my_function():
print("Hello")
print("Bye")
my_function()
- Calling Functions : 만든 함수 불러오기
def my_function():
my_function()
02. quiz
def turn_right():
turn_left()
turn_left()
turn_left()
def robot_move(x):
for i in range(x):
move()
turn_left()
move()
turn_right()
move()
turn_right()
move()
turn_left()
robot_move(6)
- 한번에 들여쓰기 단축키 : command + ]
03. 파이썬에서의 들여쓰기(indent)
- indentation : 함수 만들 때 들여쓰기를 해야 def() 함수 아래에 속하게 됨.
- https://peps.python.org/pep-0008/
- 공식 python 문서에선 space를 tab보다 우선시함.
- 파이썬에서 들여쓰기를 하려면 4칸 들여쓰기 해야 함.
- 코드 편집기에서 space = 4로 설정하면 tab 한 번에 4칸을 움직일 수 있음.
def my_function():
if sky == "clear":
print("blue")
else:
print("grey")
04. while 반복문 (While Loop)
- while : 특정 조건이 참일 경우 계속 실행되는 반복문
- for 문과 while 문 차이점
for item in list_of_items:
for number in range(a,b):
print(number)
while something_is_true
number_of_hudles = 6
while number_of_hudels >0:
jump()
number_of_hurdles -=1
print(number_of_hurdles)
def turn_right():
turn_left()
turn_left()
turn_left()
def robot_move():
move()
turn_left()
move()
turn_right()
move()
turn_right()
move()
turn_left()
while not at_goal():
robot_move()
- at_goal()이 True가 될 때까지 반복함.
- for 반복문 : 어떤 것을 반복하고,각 아이템에 대해 뭔가를 해야 할 때 유용함.
- while 반복문 : 설정한 조건에 도달할 때까지 무한 반복하고 싶을 때 유용함.
- for 문의 경우 몇 번 반복할 것인지 상한선을 정해놓기 때문에 중간에 정지되지만, while 문의 경우 특정 조건이 거짓이 되기 전까지 계속해서 무한 반복함.
04. quiz
def turn_right():
turn_left()
turn_left()
turn_left()
def jump():
turn_left()
move()
turn_right()
move()
turn_right()
move()
turn_left()
while not at_goal():
if wall_in_front():
jump()
else:
move()
05. quiz
def turn_right():
turn_left()
turn_left()
turn_left()
def jump():
turn_left()
while wall_on_right():
move()
turn_right()
move()
turn_right()
while front_is_clear():
move()
turn_left()
while not at_goal():
if wall_in_front():
jump()
else:
move()
06. quiz
def turn_right():
turn_left()
turn_left()
turn_left()
while front_is_clear():
move()
turn_left()
while not at_goal():
if right_is_clear():
turn_right()
move()
elif front_is_clear():
move()
else:
turn_left()