WeniVooks

검색

위니브 월드(선생님용)

지금까지 이런 맛은 없었다

1. 챕터의 목표

숫자 연산: 더하고, 빼고, 나누고, 곱하는 연산을 자유롭게 할 수 있습니다.

딕셔너리: 딕셔너리 자료형을 이해하고 key값을 이용하여 value값을 꺼낼 수 있습니다.

출력: f-string용법을 사용하여 원하는 형태로 출력할 수 있습니다.

2. 스토리

해골 섬에서 잡은 물고기는 살이 통통하고 맛이 일품이라 날이 갈수록 인기가 높아졌습니다. 심지어 다른 마을에서는 웃돈을 주고 생선을 사기까지 이르렀어요.

고민에 빠진 라이캣

오늘은 생선가게에 비치된 물고기를 다 팔았을 때 매출액이 얼마나 나올지 계산해 보겠습니다.

2.1 임무

생선가게 있는 모든 물고기를 줍고, fish-1은 1000노드, fish-2는 2000노드, fish-3는 3000노드에 팔 때 얼마의 매출액을 달성할 수 있는지 아래와 같이 터미널에 출력하세요. 아래 항목 중 마리는 item()을 사용해서 출력해야 하며, 합은 가격마리를 곱해서 나온 값이어야 합니다.

종류 마리 가격
fish-1 2 1000 2000
fish-2 3 2000 6000
fish-3 5 3000 15000
23000
2.2 사용 코드

아래 코드들을 조합하여 문제를 풀어주세요.

mission_start()
mission_end()
move()
repeat(2, move)
pick()
print('hello world!')
print('hello', 'world')
print(f'hello world')
item()
item()['fish-1']
10 + 10
10 - 3
10 / 3
10 // 3
10 * 3
10 ** 3
mission_start()
mission_end()
move()
repeat(2, move)
pick()
print('hello world!')
print('hello', 'world')
print(f'hello world')
item()
item()['fish-1']
10 + 10
10 - 3
10 / 3
10 // 3
10 * 3
10 ** 3

3. 문제 풀이

3.1 딕셔너리 자료형(dict)

딕셔너리는 key와 value의 쌍으로 이뤄져 있습니다. 이 자료형을 사용하면 key 값을 이용하여 value를 가져올 수 있습니다. 예를 들어 아래와 같은 코드를 실행시키면 d[’one’]은 ‘하나’를 d[’two’]는 ‘둘’을 출력합니다.

d = {'one': '하나', 'two': '둘'}
d['one']
d = {'one': '하나', 'two': '둘'}
d['one']

아래와 같은 코드라면 d[’one’]은 1을 d[’two’]는 2를 출력합니다.

d = {'one': 1, 'two': 2}
d['one']
d = {'one': 1, 'two': 2}
d['one']

type()함수를 사용하여 변수 d의 자료형을 확인해 보면 <class 'dict'>라고 출력이 됩니다. 읽을 때에는 딕셔너리라고 읽습니다.

d = {'one': 1, 'two': 2}
type(d)
d = {'one': 1, 'two': 2}
type(d)

딕셔너리의 값은 수정할 수 있습니다.

d = {'one': 1, 'two': 2}
d['one'] = 100
d
d = {'one': 1, 'two': 2}
d['one'] = 100
d

위 코드의 결과는 {'one': 100, 'two': 2}입니다.


3.2 문제풀이
Before After

이 문제는 world에서 캐릭터를 움직이는 것보다는 산술 연산이나 딕셔너리 자료형을 학습하는 데 중점을 둔 문제입니다. 앞으로 이동하며 물고기를 있는 만큼 모두 줍습니다.

mission_start()
 
move()
repeat(2, pick)
move()
repeat(5, pick)
move()
repeat(10, pick)
 
mission_end()
mission_start()
 
move()
repeat(2, pick)
move()
repeat(5, pick)
move()
repeat(10, pick)
 
mission_end()

이렇게 주운 아이템들은 item() 함수를 사용하여 호출할 수 있습니다. 노트북은 print를 하지 않고 변수나 함수를 실행하면 결괏값을 바로 아래 출력합니다.

item()
item()

이렇게 출력된 것을 보니 중괄호로 씌워져 있습니다. fish-1이 2개, fish-2가 5개, fish-3가 10개 있는 것을 확인할 수 있습니다.

이제 아래와 같이 출력해 보도록 하겠습니다.

종류   마리    가격    합
fish-1 2       1000   2000
fish-2 3       2000   6000
fish-3 5       3000   15000
23000
종류   마리    가격    합
fish-1 2       1000   2000
fish-2 3       2000   6000
fish-3 5       3000   15000
23000

위 코드는 아래와 같이 출력할 수 있습니다.

print('종류   마리    가격    합')
print('fish-1 2       1000   2000')
print('fish-2 3       2000   6000')
print('fish-3 5       3000   15000')
print('합                    23000')
print('종류   마리    가격    합')
print('fish-1 2       1000   2000')
print('fish-2 3       2000   6000')
print('fish-3 5       3000   15000')
print('합                    23000')

여기서 우리가 사용할 수 있는 변수들은 사용해 보도록 하겠습니다. 잡은 물고기 수와 합은 계산해서 넣을 수 있습니다. 여기서 item()이 dict입니다. 앞에서 배운 것과 형태가 조금 다른데요. 이 형태에 대한 얘기는 함수까지 가서 말씀을 드리도록 하겠습니다.

print(f'종류   마리    가격    합')
print(f'fish-1 {item()["fish-1"]}       1000   {item()["fish-1"] * 1000}')
print(f'fish-2 {item()["fish-2"]}       2000   {item()["fish-2"] * 2000}')
print(f'fish-3 {item()["fish-3"]}       3000   {item()["fish-3"] * 3000}')
print(f'종류   마리    가격    합')
print(f'fish-1 {item()["fish-1"]}       1000   {item()["fish-1"] * 1000}')
print(f'fish-2 {item()["fish-2"]}       2000   {item()["fish-2"] * 2000}')
print(f'fish-3 {item()["fish-3"]}       3000   {item()["fish-3"] * 3000}')

우선 합은 계산하지 않았는데요. 코드가 매우 복잡해 보이죠? 그래서 아래와 같이 미리 계산을 한 다음 넣는 것을 권합니다. 변수명을 작성할 때는 어떤 값을 나타내는지 알 수 있는 변수명으로 작성하면 가독성을 더욱 높일 수 있습니다. 가독성은 코딩에서 매우 중요한 요소입니다. 코드를 수월하게 읽고, 쉽게 이해할 수 있는 정도를 나타냅니다. fish1_count는 fish-1의 개수를 나타내고, fish_price_all은 전체 물고기 금액을 나타낸다고 쉽게 알 수 있는 것처럼요.

fish1_count = item()["fish-1"]
fish2_count = item()["fish-2"]
fish3_count = item()["fish-3"]
fish1_price = fish1_count*1000
fish2_price = fish2_count*2000
fish3_price = fish3_count*3000
fish_price_all = fish1_price + fish2_price + fish3_price
print(f'종류   마리    가격    합')
print(f'fish-1 {fish1_count}       1000   {fish1_price}')
print(f'fish-2 {fish2_count}       2000   {fish2_price}')
print(f'fish-2 {fish3_count}       3000   {fish3_price}')
print(f'합                    {fish_price_all}')
fish1_count = item()["fish-1"]
fish2_count = item()["fish-2"]
fish3_count = item()["fish-3"]
fish1_price = fish1_count*1000
fish2_price = fish2_count*2000
fish3_price = fish3_count*3000
fish_price_all = fish1_price + fish2_price + fish3_price
print(f'종류   마리    가격    합')
print(f'fish-1 {fish1_count}       1000   {fish1_price}')
print(f'fish-2 {fish2_count}       2000   {fish2_price}')
print(f'fish-2 {fish3_count}       3000   {fish3_price}')
print(f'합                    {fish_price_all}')

4. 정답 코드

초기화 후 한 번에 실행시킬 수 있는 정답 코드입니다.

mission_start()
 
move()
repeat(2, pick)
move()
repeat(5, pick)
move()
repeat(10, pick)
item()
fish1_count = item()["fish-1"]
fish2_count = item()["fish-2"]
fish3_count = item()["fish-3"]
fish1_price = fish1_count*1000
fish2_price = fish2_count*2000
fish3_price = fish3_count*3000
fish_price_all = fish1_price + fish2_price + fish3_price
print(f'종류   마리    가격    합')
print(f'fish-1 {fish1_count}       1000   {fish1_price}')
print(f'fish-2 {fish2_count}       2000   {fish2_price}')
print(f'fish-2 {fish3_count}      3000   {fish3_price}')
print(f'합                    {fish_price_all}')
 
mission_end()
mission_start()
 
move()
repeat(2, pick)
move()
repeat(5, pick)
move()
repeat(10, pick)
item()
fish1_count = item()["fish-1"]
fish2_count = item()["fish-2"]
fish3_count = item()["fish-3"]
fish1_price = fish1_count*1000
fish2_price = fish2_count*2000
fish3_price = fish3_count*3000
fish_price_all = fish1_price + fish2_price + fish3_price
print(f'종류   마리    가격    합')
print(f'fish-1 {fish1_count}       1000   {fish1_price}')
print(f'fish-2 {fish2_count}       2000   {fish2_price}')
print(f'fish-2 {fish3_count}      3000   {fish3_price}')
print(f'합                    {fish_price_all}')
 
mission_end()

5. 심화 코드

중고급 심화 과정 학생들이 다룰 수 있는 코드입니다. 심화 코드는 여러 개념을 복합 설명해야 하므로 설명을 덧붙이지 않습니다.

아래 코드는 최종 합만을 출력하고 있습니다. 출력 형식에 맞춰 코드를 작성해 주세요.

mission_start()
 
while front_is_clear():
    move()
    while on_item():
        pick()
 
fish = ['fish-1','fish-2','fish-3']
price = [1000, 2000, 3000]
result = 0
for i in range(len(fish)):
    key = fish[i]
    if key in item():
        result += price[i]*item()[key]
 
print(result)
 
mission_end()
mission_start()
 
while front_is_clear():
    move()
    while on_item():
        pick()
 
fish = ['fish-1','fish-2','fish-3']
price = [1000, 2000, 3000]
result = 0
for i in range(len(fish)):
    key = fish[i]
    if key in item():
        result += price[i]*item()[key]
 
print(result)
 
mission_end()
2.3 해골섬으로 향하는 라이캣2.5 직원의 승진