출항 준비
이 단계에서는 선원 마법사가 여정을 시작하기 위해 돛을 올리는 것을 돕게 됩니다. 시작하기 위해, 완벽한 바람 방향을 찾을 때까지 while 루프를 사용하여 매일 바람 방향을 확인하는 주문 (스크립트) 을 만들 것입니다. 무작위 바람 방향을 생성하고 루프에서 테스트하여 이를 시뮬레이션합니다.
선호하는 편집기를 사용하여 /home/labex/project/wind_checker.py 파일을 편집하고 다음 코드를 삽입하십시오.
import random
## 바람 방향 정의
possible_directions = ["north", "south", "east", "west"]
## 완벽한 방향을 동쪽으로 설정
perfect_direction = "east"
## while 루프 시작
while True:
## 바람 방향 확인 시뮬레이션
current_direction = random.choice(possible_directions)
print(f"바람이 {current_direction}에서 불어옵니다.")
## 바람 방향이 완벽한지 확인
if current_direction == perfect_direction:
print("어이! 돛을 올리기에 완벽한 바람입니다!")
break
else:
print("항해에 적합하지 않습니다. 내일 다시 확인해 봅시다.")
스크립트를 실행합니다:
python wind_checker.py
이 스크립트는 current_direction이 "east"로 설정된 perfect_direction과 일치할 때까지 무한정 루프를 실행합니다. 루프가 실행될 때마다 새로운 날을 기다리고 바람 방향을 다시 확인하는 것을 시뮬레이션합니다.
다음 정보가 터미널에 표시되어야 합니다.
The wind blows from the north.
Not favorable for sailing. Let's check again tomorrow.
The wind blows from the west.
Not favorable for sailing. Let's check again tomorrow.
The wind blows from the east.
Ahoy! Perfect wind for setting sails!