ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • Python - basic while문
    Python 2021. 2. 9. 03:46
    728x90

    while문

    i = 1
    sum = 0
    while i <= 100:
        if i % 3 == 0:
            if i % 2 != 0:
                print(i)
                sum = sum + i
        i = i + 1
    print('total = ',sum)

    Q. 1~100까지의 숫자 중 3의 배수이면서 2의 배수가 아닌 수를 출력하라.

       그리고 그 합계를 출력하라.

    [출력결과]

    3
    9
    15
    21
    27
    33
    39
    45
    51
    57
    63
    69
    75
    81
    87
    93
    99
    total =  867

     

    커피 자판기

    coffee = 10
    while True:
    	money = int(input("돈을 넣어주세요: "))
        if money == 300:
        	print("커피를 줍니다")
        	coffee = coffee - 1
        elif money > 300:
        	print("거스름돈 %d를 주고 커피를 줍니다."%(money - 300))
            coffee = coffee -1
        else:
        	print("돈을 다시 돌려주고 커피를 주지 않습니다")
            print("남은 커피의 양은 %d개 입니다." %coffee)
        if coffee == 0:
        	print("커피가 다 떨어졌습니다. 판매를 중지합니다.")
            break

    [출력결과]

    돈을 넣어주세요: 1000
    거스름돈 700를 주고 커피를 줍니다.
    돈을 넣어주세요: 

     

    구구단 전체 출력하기

    dan = 1
    while (dan < 9):
        dan = dan + 1
        num = 1
        while (num < 10):
            print(dan,"x", num,"=",dan*num) 
            num = num + 1
        print() 

     

    while - continue, break

    a = 2
    while a < 10:
        a += 1
        if a == 5:continue
        if a == 9:break
        print(a, end = " ")
    else:
        print('\n while 정상수행')
    print('\n while 수행 후 %d'%a)

    [출력 결과]

    3 4 6 7 8 
     while 수행 후 9

    'Python' 카테고리의 다른 글

    Python - basic for문에 format 사용  (0) 2021.02.09
    Python - basic For문 활용  (0) 2021.02.09
    Python - basic if문 조건 반복문  (0) 2021.02.09
    Python- basic 쟈료형타입 4가지  (0) 2021.02.09
    Python - basic type isinstance  (0) 2021.02.09
Designed by Tistory.