오늘은
부족한 while반복문에 대해서 공부를 했다.
이번에 게임 만들기 과제를 하면서 개인, 팀으로 진행했는데 게임을 진행시킬 때 반복문을 많이 사용했기에 공부를 추가로 하게 됐다. 주로 궁금했던 게 continue랑 break가 이해가 안 됐다. 추가로 if문도 같이 공부하게 됐다.
while True:
while문 시작은 True로 시작해서 무한하게 반복한다. 그래서 시작된 무한루프 ㅠㅠㅠ 탈출하고 싶어
그때사용하는 게 continue와 break라고 공부했다. continue는 다시 처음으로 돌아가고 break는 반복문탈출 조건문에 추가해 줘서 해당조건을 만족하고 안하고의 차이로 반복과 멈춤이 공존한다.
작업 과정
if use_potion == "1":
if inventory["회복물약"] == 0:
time.sleep(1)
print("\033[91m해당 포션이 존재하지 않습니다.\033[0m")
time.sleep(1)
continue
elif inventory["회복물약"] > 0:
other.hp = min(
round(other.hp + other.max_hp*0.5), other.max_hp)
player_inventory.removeItem("회복물약")
time.sleep(1)
print(f"{other.name}의 hp가 50% 회복됐습니다.")
elif use_potion == "2":
if inventory["완전회복물약"] == 0:
time.sleep(1)
print("\033[91m해당 포션이 존재하지 않습니다.\033[0m")
time.sleep(1)
continue
elif inventory["완전회복물약"] > 0:
other.hp == other.max_hp
player_inventory.removeItem("완전회복물약")
time.sleep(1)
print(f"{other.name}의 hp가 완전히 회복됐습니다.")
elif use_potion == "3":
if inventory["공격물약"] == 0:
time.sleep(1)
print("\033[91m해당 포션이 존재하지 않습니다.\033[0m")
time.sleep(1)
continue
elif inventory["공격물약"] > 0:
other.power = math.ceil(other.power*1.1)
player_inventory.removeItem("공격물약")
time.sleep(1)
print(f"{other.name}의 공격력이 10% 상승합니다.")
elif use_potion == "4":
if inventory["속도물약"] == 0:
time.sleep(1)
print("\033[91m해당 포션이 존재하지 않습니다.\033[0m")
time.sleep(1)
continue
결말
위에 코드로 확인할 수 있는 부분은 continue를 사용하면 해당조건을 만족했을 경우 다시 조건문이 돌고 다시 조건을 입력하고 새로 출력가능하다. 예를 들어서 인벤토리에 회복물약이 0이면 다시 처음조건으로 이동하고 회복물약이 0보다 크면 인벤토리에서 물약을지 운다.
def removeItem(self, item):
if item in inventory:
inventory[item] -= 1
print(f"\033[32m{item} 를 사용하였습니다.\033[0m")
리무브 함수에서 인벤토리에서 1개씩 제거하는 함수를 추가해 줬기에 위 글에서 1개씩 지우는 부분을 해결해 줬다.
if inventory["회복물약"] == 0:
time.sleep(1)
print("\033[91m해당 포션이 존재하지 않습니다.\033[0m")
time.sleep(1)
continue
elif inventory["회복물약"] > 0:
other.hp = min(
round(other.hp + other.max_hp*0.5), other.max_hp)
player_inventory.removeItem("회복물약")
time.sleep(1)
print(f"{other.name}의 hp가 50% 회복됐습니다.")
continue와 break를 과제를 하면서 배웠다.
오늘의 규파팡이 느낀 점!
과제와 프로젝트를 할수록 실력?이 늘어가는 게 느껴진다 위에서 break를 설명하지 않았지만 break는 조건문 밖으로 나가고 아예 while 반복문자체를 종료시켜 준다. 이렇게 쌓이면 언젠간 개발자가 되어있지 않을까? 그리고 오늘의 나를 귀엽게 봐 줄 미래의 규파팡이 궁금하다.
'개발일지? > 내배캠' 카테고리의 다른 글
캠프 4주 1일차 TIL (0) | 2023.04.03 |
---|---|
WIL 3주차 - 내배캠 (2) | 2023.04.02 |
캠프 3주 4일차 TIL (0) | 2023.03.30 |
캠프 3주 3일차 TIL (0) | 2023.03.30 |
캠프 3주 1일차 TIL (0) | 2023.03.27 |