제어#
소개#
파이썬을 사용한 제어를 통해 로봇에게 언제 기다려야 하는지, 언제 동작을 반복해야 하는지, 어떻게 결정을 내려야 하는지, 그리고 언제 프로젝트를 종료해야 하는지를 알려줄 수 있습니다.
다음은 메서드와 핵심 파이썬 키워드를 포함한 모든 컨트롤 목록입니다.
wait– 지정된 시간 동안 프로젝트를 일시 중지합니다.for– 시퀀스의 각 항목에 대해 코드를 반복합니다.if– 조건이 참이면 코드를 실행합니다.if/else– 조건에 따라 다른 코드를 실행합니다.if/elif/else– 여러 조건을 순서대로 확인합니다.while– 조건이 참인 동안 코드를 반복합니다.break– 루프를 즉시 종료합니다.stop_program– 실행 중인 프로그램을 종료합니다.pass– 작업이 필요하지 않을 때 사용되는 플레이스홀더.
기다리다#
wait 합니다.
Usage:wait(time, units)
매개변수 |
설명 |
|---|---|
|
기다려야 하는 시간을 양의 정수 또는 소수로 나타내십시오. |
|
대기 시간을 나타내는 단위: |
# Move right for one second, then stop
robot.move_at(90)
wait(1, SECONDS)
robot.stop_all_movement()
을 위한#
for 특정 횟수만큼 일련의 작업을 반복합니다. for 목록, 튜플, 사전, 세트, 문자열의 항목을 반복하거나 range를 사용하여 지정된 루프 횟수만큼 반복하는 데 사용할 수 있습니다.
사용 횟수:
for value in expression_list:
pass
구성 요소 |
설명 |
|---|---|
|
반복문 내 현재 요소를 저장하는 임시 변수입니다. |
|
반복 대상이 되는 요소들의 모음(예: 리스트, 문자열, 범위). |
# Move in a square path.
for index in range(4):
robot.move_for(50, 0)
robot.turn_for(RIGHT, 90)
# Print each item in the list
colors = ["Red", "Green", "Blue"]
for color in colors:
robot.screen.print(color)
robot.screen.next_row()

만약에#
if 조건이 참이면 들여쓰기된 코드 블록을 실행합니다. True.
용법:
if condition:
pass
구성 요소 |
설명 |
|---|---|
|
문장이 실행될 때 검사되는 표현식 또는 변수입니다. |
# Kick when the screen is pressed
while True:
if robot.screen.pressing():
robot.kicker.kick(MEDIUM)
wait(0.1, SECONDS)
만약/그렇지 않으면#
if 이고 else . 조건이 True False 에 따라 들여쓰기된 코드 블록 중 어느 것이 실행될지 결정합니다.
용법:
if condition:
pass
else:
pass
구성 요소 |
설명 |
|---|---|
|
An expression or variable that is checked when the statement runs. If it is |
# Show one emoji when the screen is pressed,
# and a different emoji when not pressed
while True:
if robot.screen.pressing():
robot.screen.show_emoji(EXCITED)
else:
robot.screen.show_emoji(BORED)
wait(0.1, SECONDS)
만약/엘리프/엘프#
if/elif/else 구조는 조건에 따라 어떤 들여쓰기된 코드 블록이 실행될지 선택합니다.
if이고, 조건이True.elif이전 조건이 모두 거짓인 경우에만 추가 조건을 확인합니다.False여러 개의elif문을 사용할 수 있습니다.else이전 조건 중 어느 것도 참이 아닌 경우에만 해당 블록을 실행합니다.True.
용법:
if condition:
pass
elif condition:
pass
else:
pass
구성 요소 |
설명 |
|---|---|
|
문장이 실행될 때 검사되는 표현식 또는 변수입니다. 첫 번째 조건이 참 |
# Move the robot forward or reverse
# based on the position of the joystick
def when_axis_changed():
position = controller.axis1.position()
if position > 0:
robot.move_at(0)
elif position < 0:
robot.move_at(180)
else:
robot.stop_all_movement()
controller.axis1.changed(when_axis_changed)
하는 동안#
while 조건이 True코드를 반복적으로 실행합니다. True 을 조건으로 사용하면 “무한 실행”처럼, not 조건에 추가하면 아래 예시와 같이 “조건이 참이 될 때까지 대기”처럼 사용할 수 있습니다.
용법:
while condition:
pass
구성 요소 |
설명 |
|---|---|
|
각 반복 전에 확인하는 표현식 또는 변수입니다. 값이 |
# Keep the LEDs green while the robot is moving
robot.move_for(200, 0, wait=False)
while robot.is_move_active():
robot.led.on(ALL_LEDS, GREEN)
wait(50, MSEC)
robot.led.on(ALL_LEDS, BLACK)
while True:
# Continually flash all LEDs red then green.
robot.led.on(ALL_LEDS, RED)
wait(0.5, SECONDS)
robot.led.on(ALL_LEDS, GREEN)
wait(0.5, SECONDS)
# Wait until the screen is pressed before
# turning off the LEDs
while not robot.screen.pressing():
robot.led.on(ALL_LEDS, RED)
wait(50, MSEC)
robot.led.on(ALL_LEDS, BLACK)
부서지다#
break 루프를 즉시 종료합니다. break 무한 루프에 빠질 수 있는 루프를 종료하는 데 사용할 수 있습니다.
# Flash LEDs until the screen is pressed
while True:
robot.led.on(ALL_LEDS, RED)
wait(0.5, SECONDS)
robot.led.on(ALL_LEDS, GREEN)
wait(0.5, SECONDS)
if robot.screen.pressing():
break
# Turn the LEDs Off
robot.led.off(ALL_LEDS)
프로그램 중지#
stop_program 실행 중인 프로젝트를 종료합니다.
Usage:robot.stop_program()
매개변수 |
설명 |
|---|---|
이 메서드에는 매개변수가 없습니다. |
# Stop the project once the screen is pressed
while True:
robot.led.on(ALL_LEDS, RED)
wait(0.5, SECONDS)
robot.led.on(ALL_LEDS, GREEN)
wait(0.5, SECONDS)
if robot.screen.pressing():
robot.stop_program()
통과하다#
pass 은 향후 코드를 위한 자리 표시자이며, 빈 반복문, 조건문 및 함수에서 발생하는 오류를 방지하는 데 사용할 수 있습니다.
if condition:
pass
def function():
pass