본문 바로가기
코딩 연습/코딩배우기

[python] 파이썬 알고리즘 - 별표 찍기

by good4me 2020. 10. 14.

goodthings4me.tistory.com

 

#모양 1

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

for i in range(1, 6):
    for j in range(1, i + 1):
        print('*', end = '')
    print()


# 또는

for i in range(1, 6):
    print('*' * i)
  

 

#모양 2

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

for i in range(1, 6):
    for j in range(1,7 - i):
        print('*', end = '')
    print()


# 또는

for i in range(5, 0, -1):
    print('*' * i)
    

good4me.co.kr

 

#모양 3

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

for i in range(1, 6):
    for j in range(1, 6 - i):
        print(' ', end = '')
    for k in range(1, i + 1):
        print('*', end = '')
    print()
    
    
    # 또는
    
    for i in range(5, 0, -1):
    print(' ' * (i - 1), end = '')
    print('*' * (6 - i))
    
    

 

#모양 4

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

for i in range(1, 6):
    for k in range(1, i):
        print(' ', end = '')
    for j in range(1, 7 - i):
        print('*', end = '')
    print()
    
    
    # 또는
    
    for i in range(5, 0, -1):
    print(' ' * (5 - i), end = '')
    print('*' * i)
    

 

#모양 5

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

    for j in range(1, 6 - i):
        print(' ', end = '')
    for k in range(1, i * 2, 1):
        print('*', end = '')
    print()
    
    
    # 또는
    
    for i in range(5, 0, -1):
    print(' ' * (i - 1), end = '')
    print('*' * ((6 - i) * 2 - 1))
    

 

 

댓글