fresh_fruit = { '사과': 10, '바나나': 8, '레몬': 5, } def make_lemonade(count): pass def out_of_stock(): pass # count 변수가 if문에서만 쓰이기 떄문에 대입식(왈러스 연산자)를 통해 if문 줄에 보이게 if count := fresh_fruit.get('레몬', 0): make_lemonade(count) else: out_of_stock() # 아래 방법은 count 변수가 더 이상 강조되지 않는다 pieces = 0 if (count := fresh_fruit.get('바나나', 0)) >= 2: pieces = slice_bananas(count) try: smoothies = make_smoothies(pieces) except OutOfBananas: out_of_stock() # 대입식을 사용하면 네스팅 없이 깔끔하게 switch/case문과 같이 만들수 있다 if (count := fresh_fruit.get('바나나', 0)) >= 2: pieces = slice_bananas(count) to_enjoy = make_smoothies(pieces) elif (count := fresh_fruit.get('사과', 0)) >= 4: to_enjoy = make_cider(count) elif count := fresh_fruit.get('레몬', 0): to_enjoy = make_lemonade(count) else: to_enjoy = '아무것도 없음' bottles = [] while True: # 무한루프의 제어가 break문에 책임이 있어 잘못하면 평생 나오지 못한다 fresh_fruit = pick_fruit() if not fresh_fruit: # 중간에서 끝내기 break for fruit, count in fresh_fruit.items(): batch = make_juice(fruit, count) bottles.extend(batch) # 대입식을 이용하면 루프 제어에 용이하다 bottles = [] while fresh_fruit := pick_fruit(): for fruit, count in fresh_fruit.items(): batch = make_juice(fruit, count) bottles.extend(batch) 참고자료 : LEGB