이중 for문

쉬운 예제 : 구구단표

for문으로 2단 만들기

출처 : [ Python 기초 ] 반복문1 (for문, 이중 for문)

https://velog.io/@hj5730/Python-%EA%B8%B0%EC%B4%88-%EB%B0%98%EB%B3%B5%EB%AC%B81-for%EB%AC%B8-%EC%9D%B4%EC%A4%91-for%EB%AC%B8

1
2
3
4
5
a = 2

for b in range(1, 10):
c = a * b
print('%d X %d = %d' % (a, b, c))
2 X 1 = 2
2 X 2 = 4
2 X 3 = 6
2 X 4 = 8
2 X 5 = 10
2 X 6 = 12
2 X 7 = 14
2 X 8 = 16
2 X 9 = 18

이중 for문으로 전체 구구단표 만들기

1
2
3
4
5
6
7
8
print('-' * 50)

for a in range(2, 10):
for b in range(1, 10):
c = a * b
print('%d x %d = %d' % (a, b, c))

print('-' * 50)
--------------------------------------------------
2 x 1 = 2
2 x 2 = 4
2 x 3 = 6
2 x 4 = 8
2 x 5 = 10
2 x 6 = 12
2 x 7 = 14
2 x 8 = 16
2 x 9 = 18
--------------------------------------------------
3 x 1 = 3
3 x 2 = 6
3 x 3 = 9
3 x 4 = 12
3 x 5 = 15
3 x 6 = 18
3 x 7 = 21
3 x 8 = 24
3 x 9 = 27
--------------------------------------------------
4 x 1 = 4
4 x 2 = 8
4 x 3 = 12
4 x 4 = 16
4 x 5 = 20
4 x 6 = 24
4 x 7 = 28
4 x 8 = 32
4 x 9 = 36
--------------------------------------------------
5 x 1 = 5
5 x 2 = 10
5 x 3 = 15
5 x 4 = 20
5 x 5 = 25
5 x 6 = 30
5 x 7 = 35
5 x 8 = 40
5 x 9 = 45
--------------------------------------------------
6 x 1 = 6
6 x 2 = 12
6 x 3 = 18
6 x 4 = 24
6 x 5 = 30
6 x 6 = 36
6 x 7 = 42
6 x 8 = 48
6 x 9 = 54
--------------------------------------------------
7 x 1 = 7
7 x 2 = 14
7 x 3 = 21
7 x 4 = 28
7 x 5 = 35
7 x 6 = 42
7 x 7 = 49
7 x 8 = 56
7 x 9 = 63
--------------------------------------------------
8 x 1 = 8
8 x 2 = 16
8 x 3 = 24
8 x 4 = 32
8 x 5 = 40
8 x 6 = 48
8 x 7 = 56
8 x 8 = 64
8 x 9 = 72
--------------------------------------------------
9 x 1 = 9
9 x 2 = 18
9 x 3 = 27
9 x 4 = 36
9 x 5 = 45
9 x 6 = 54
9 x 7 = 63
9 x 8 = 72
9 x 9 = 81
--------------------------------------------------

다른 예제

https://wikidocs.net/104141

  • end=” “ -> 한줄로 표현해주기 위한 것이다.
1
2
3
4
for i in range(10):
for j in range(1, 11):
print(j, end=" ")
print()
1 2 3 4 5 6 7 8 9 10 
1 2 3 4 5 6 7 8 9 10 
1 2 3 4 5 6 7 8 9 10 
1 2 3 4 5 6 7 8 9 10 
1 2 3 4 5 6 7 8 9 10 
1 2 3 4 5 6 7 8 9 10 
1 2 3 4 5 6 7 8 9 10 
1 2 3 4 5 6 7 8 9 10 
1 2 3 4 5 6 7 8 9 10 
1 2 3 4 5 6 7 8 9 10 
1
2
3
4
for i in range(10):
for j in range(1, 11):
print(j, end=" ")
print("->>>>", i)
1 2 3 4 5 6 7 8 9 10 ->>>> 0
1 2 3 4 5 6 7 8 9 10 ->>>> 1
1 2 3 4 5 6 7 8 9 10 ->>>> 2
1 2 3 4 5 6 7 8 9 10 ->>>> 3
1 2 3 4 5 6 7 8 9 10 ->>>> 4
1 2 3 4 5 6 7 8 9 10 ->>>> 5
1 2 3 4 5 6 7 8 9 10 ->>>> 6
1 2 3 4 5 6 7 8 9 10 ->>>> 7
1 2 3 4 5 6 7 8 9 10 ->>>> 8
1 2 3 4 5 6 7 8 9 10 ->>>> 9

공포의 별찍기 문제

  • 단서
1
2
3
4
for i in range(1, 11):
for j in range(1, i+1):
print('*', end="")
print()
*
**
***
****
*****
******
*******
********
*********
**********

실습1

  • 위의 별을 좌우반전으로 찍어보자.
1
2
3
4
for i in range(10):
for j in range(1, i+1):
print(" " * i + '*', end="")
print()
 *
  *  *
   *   *   *
    *    *    *    *
     *     *     *     *     *
      *      *      *      *      *      *
       *       *       *       *       *       *       *
        *        *        *        *        *        *        *        *
         *         *         *         *         *         *         *         *         *
  • 원하는 답이 아니다..
1
2
3
4
for i in range(10):
for j in range(1, i+1):
print(" " * (10-i) + '*', end="")
print()
         *
        *        *
       *       *       *
      *      *      *      *
     *     *     *     *     *
    *    *    *    *    *    *
   *   *   *   *   *   *   *
  *  *  *  *  *  *  *  *
 * * * * * * * * *
  • 막 이상하게 나온다..
1
2
3
4
for i in range(10):
for j in range(1, i+1):
print(" " * (9-i) + '*', end="")
print()
        *
       *       *
      *      *      *
     *     *     *     *
    *    *    *    *    *
   *   *   *   *   *   *
  *  *  *  *  *  *  *
 * * * * * * * *
*********

강의

  • 바로 for문부터 들어가지 마라
  • 한 줄씩 쳐보고 패턴을 파악해라
1
2
3
print(" " * 9, "*" * 1)
print(" " * 8, "*" * 2)
print(" " * 7, "*" * 3)
          *
         **
        ***
1
2
3
4
for i in range(10):
for j in range(1, i+1):
print(" " * (9-i) + "*" * j, end="")
print()
        *
       *       **
      *      **      ***
     *     **     ***     ****
    *    **    ***    ****    *****
   *   **   ***   ****   *****   ******
  *  **  ***  ****  *****  ******  *******
 * ** *** **** ***** ****** ******* ********
*********************************************
  • 이상해졌는데?
1
2
3
4
for i in range(10):
for j in range(1, i+1):
print(" " * (10-j), "*" * j)
print()
          *

          *
         **

          *
         **
        ***

          *
         **
        ***
       ****

          *
         **
        ***
       ****
      *****

          *
         **
        ***
       ****
      *****
     ******

          *
         **
        ***
       ****
      *****
     ******
    *******

          *
         **
        ***
       ****
      *****
     ******
    *******
   ********

          *
         **
        ***
       ****
      *****
     ******
    *******
   ********
  *********
  • 어? 다왔는데?
1
2
3
for j in range(1, 11):
print(" " * (10-j), "*" * j)

          *
         **
        ***
       ****
      *****
     ******
    *******
   ********
  *********
 **********

와 결국해냈당. 6번 시도후 완성했다.

Author

HS

Posted on

2022-03-31

Updated on

2022-03-31

Licensed under

You need to set install_url to use ShareThis. Please set it in _config.yml.
You forgot to set the business or currency_code for Paypal. Please set it in _config.yml.

Comments

You forgot to set the shortname for Disqus. Please set it in _config.yml.
You need to set client_id and slot_id to show this AD unit. Please set it in _config.yml.