ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 반복문 - while
    파이썬 Python/데이터 분석 2022. 2. 9. 20:00

    continue : for, while문에서 continue를 만나면, 조건식으로 이동한다.

    break : for, while문에서 break를 만나면, for나 while문에서 빠져나온다.

     

    # 1~10까지 합
    tot = 0
    i = 1
    
    while True:
        if (i>10): break
        tot += i
        i += 1
    print('tot = ', tot)

    tot =  55

     

    # 1~8까지 6,7빼고 출력
    x = 1
    
    while True :
        print(x, end=' ')
        x += 1
        if x == 6 :
                x += 2
                continue
        if x == 9 :
                break

    1 2 3 4 5 8 

     

    '''
    1~100사이의 숫자를 한 개 입력받은 후, 
    1~100사이에서 입력받은 수의 배수를 출력한 후, 개수를 구하는 프로그램을 작성하시오.
    
    [실행결과]
    1~100 사이의 배수를 구할 숫자 입력 : 7
    7 14 21 28 35 42 49 56 63 70 77 84 91 98 
    1~100 사이의7의 배수 개수 = 14
    '''
    # 나의 답안
    num = int(input('1~100 사이의 배수를 구할 숫자 입력 : '))
    count = 0
    
    for i in range(1, 100):
        if(num*i > 100) : break
        print(num * i, end=' ')
        count += 1
    print()
    print('1~100 사이의 %d의 배수 개수 = %d' %(num,count))
    
    
    # 예시답안
    num = int(input('1~100 사이의 배수를 구할 숫자 입력 : '))
    count = 0
    
    for a in range(1, 101):
        if(a % num == 0):
            print(a, end=" ")
            count += 1
    print()
    print('1~100 사이의 %d의 배수 개수 = %d' %(num,count))

    '파이썬 Python > 데이터 분석' 카테고리의 다른 글

    파이썬 확장 패키지 : numpy (2)  (0) 2022.02.15
    파이썬 확장 패키지 : numpy (1)  (0) 2022.02.14
    반복문 - for  (0) 2022.02.08
    변수, 입력  (0) 2022.02.07
    Python 특징 및 설치  (0) 2022.02.07
Designed by Tistory.