Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

3. 파이썬의 반복문

1. 1~5까지의 정수가 출력되는 코드

============= RESTART: E:\TEST\test.py ============
1
2
3
4
5
답(안) 1: while문 사용
i = 1
while i <= 5 :
    print(i)
    i = i + 1
답(안) 2: for문 사용
for i in [1, 2, 3, 4, 5] :
    print(i)

2. 1 ~ 100까지의 정수가 출력되는 코드

============= RESTART: E:\TEST\test.py ============
1
2
3
...
100
답(안) 1: while문 사용
i = 1
while i <= 100 :
    print(i)
    i = i + 1
답(안) 2: for문 사용
a = range(1, 101)  # 1 ~ 100까지의 정수 리스트를 만든다. range()는 정수 리스트를 만드는 함수. 
for i in a :
    print(i)

3. 다음과 같이 오늘의 출근 일자를 리스트로 가져왔을 때 지각한 사람이 누구인지 출력하는 코드

morning = [ ["홍길동", "08:45"], ["나지적", "08:37"], ["박문수", "09:11"] ]
============= RESTART: E:\TEST\test.py ============
지각한 사람은 박문수
답(안) for 문
morning = [ ["홍길동", "08:45"], ["나지적", "08:37"], ["박문수", "09:11"] ]

for item in morning :
    if "09:00" < item[1] :
        print("지각한 사람은 " + item[0])
답(안) while 문
morning = [ ["홍길동", "08:45"], ["나지적", "08:37"], ["박문수", "09:11"] ]

for item in morning :
    if "09:00" < item[1] :
        print("지각한 사람은 " + item[0])

4. 구구단 프로그램

=================================== RESTART: E:\test\test.py ==================================
2 * 1 = 2
2 * 2 = 4
2 * 3 = 6
2 * 4 = 8
2 * 5 = 10
2 * 6 = 12
2 * 7 = 14
2 * 8 = 16
2 * 9 = 18
3 * 1 = 3
3 * 2 = 6
3 * 3 = 9
3 * 4 = 12
3 * 5 = 15
3 * 6 = 18
3 * 7 = 21
3 * 8 = 24
3 * 9 = 27
4 * 1 = 4
4 * 2 = 8
4 * 3 = 12
4 * 4 = 16
4 * 5 = 20
4 * 6 = 24
4 * 7 = 28
4 * 8 = 32
4 * 9 = 36
5 * 1 = 5
5 * 2 = 10
5 * 3 = 15
5 * 4 = 20
5 * 5 = 25
5 * 6 = 30
5 * 7 = 35
5 * 8 = 40
5 * 9 = 45
6 * 1 = 6
6 * 2 = 12
6 * 3 = 18
6 * 4 = 24
6 * 5 = 30
6 * 6 = 36
6 * 7 = 42
6 * 8 = 48
6 * 9 = 54
7 * 1 = 7
7 * 2 = 14
7 * 3 = 21
7 * 4 = 28
7 * 5 = 35
7 * 6 = 42
7 * 7 = 49
7 * 8 = 56
7 * 9 = 63
8 * 1 = 8
8 * 2 = 16
8 * 3 = 24
8 * 4 = 32
8 * 5 = 40
8 * 6 = 48
8 * 7 = 56
8 * 8 = 64
8 * 9 = 72
9 * 1 = 9
9 * 2 = 18
9 * 3 = 27
9 * 4 = 36
9 * 5 = 45
9 * 6 = 54
9 * 7 = 63
9 * 8 = 72
9 * 9 = 81
답(안)
a = 2
while a <= 9 :
    b = 1

    while b <= 9 :
        c = a * b
        msg = "{0} * {1} = {2}".format(a, b, c)
        print(msg)

        b = b + 1
    a = a + 1

5. 위 구구단 프로그램에 단마다 구분하기

=================================== RESTART: E:\test\test.py ==================================
2 * 1 = 2
2 * 2 = 4
2 * 3 = 6
2 * 4 = 8
2 * 5 = 10
2 * 6 = 12
2 * 7 = 14
2 * 8 = 16
2 * 9 = 18

3 * 1 = 3
3 * 2 = 6
3 * 3 = 9
3 * 4 = 12
3 * 5 = 15
3 * 6 = 18
3 * 7 = 21
3 * 8 = 24
3 * 9 = 27

4 * 1 = 4
4 * 2 = 8
4 * 3 = 12
4 * 4 = 16
4 * 5 = 20
4 * 6 = 24
4 * 7 = 28
4 * 8 = 32
4 * 9 = 36

5 * 1 = 5
5 * 2 = 10
5 * 3 = 15
5 * 4 = 20
5 * 5 = 25
5 * 6 = 30
5 * 7 = 35
5 * 8 = 40
5 * 9 = 45

6 * 1 = 6
6 * 2 = 12
6 * 3 = 18
6 * 4 = 24
6 * 5 = 30
6 * 6 = 36
6 * 7 = 42
6 * 8 = 48
6 * 9 = 54

7 * 1 = 7
7 * 2 = 14
7 * 3 = 21
7 * 4 = 28
7 * 5 = 35
7 * 6 = 42
7 * 7 = 49
7 * 8 = 56
7 * 9 = 63

8 * 1 = 8
8 * 2 = 16
8 * 3 = 24
8 * 4 = 32
8 * 5 = 40
8 * 6 = 48
8 * 7 = 56
8 * 8 = 64
8 * 9 = 72

9 * 1 = 9
9 * 2 = 18
9 * 3 = 27
9 * 4 = 36
9 * 5 = 45
9 * 6 = 54
9 * 7 = 63
9 * 8 = 72
9 * 9 = 81
답(안)
a = 2
while a <= 9 :
    b = 1

    while b <= 9 :
        c = a * b
        msg = "{0} * {1} = {2}".format(a, b, c)
        print(msg)

        b = b + 1
    a = a + 1
    print("")

6. 좋아하는 과일로 리스트를 만들고 ’바나나’가 있으면 ‘미끄러져 조심해’ 라고 출력하고, 없으면 ‘바나나는 왜 없어’ 라고 출력하는 코드

답(안)
fruits = ['사과', '딸기', '바나나', '망고']

find_banana = False

for item in fruits :
    if item == '바나나' :
        find_banana = True

if find_banana == True :
    print("미끄러져 조심해")
else :
    print("바나나는 왜 없어")

7. 좋아하는 과일로 리스트를 만들고 ’바나나’가 있으면 ‘미끄러져 조심해’ 라고 출력하고, 더 이상 리스트를 조사하지 않는다. 바나나가 없으면 ‘바나나는 왜 없어’ 라고 출력하는 코드

답(안) 1
fruits = ['사과', '딸기', '바나나', '망고']

find_banana = False

for item in fruits :
    if item == '바나나' :
        find_banana = True
        break

if find_banana == True :
    print("미끄러져 조심해")
else :
    print("바나나는 왜 없어")
답(안) 2
fruits = ['사과', '딸기', '바나나', '망고']

find_banana = False

for item in fruits :
    print ("{0} 검사 중...".format(item))
    if item == '바나나' :
        find_banana = True
        break

if find_banana == True :
    print("미끄러져 조심해")
else :
    print("바나나는 왜 없어")

8. 좋아하는 과일로 리스트를 만들고, 리스트의 각 아이템마다 “OOO 잼 만들기“를 출력하되 아이템이 ‘바나나’ 일 때만 그렇게 출력하지 않는 코드

답(안)
fruits = ['사과', '딸기', '바나나', '망고']

for item in fruits :
    if item == '바나나' :
        continue

    print("{0} 잼 만들기".format(item) )